<C#任务导引教程>练习十
/*83,使用接口完成多继承问题 简化版*/
using System;
interface ITeacher
{
string Name
{
get;
set;
}
int Age
{
get;
set;
}
string Title
{
get;
set;
}
}
interface IStudent
{
string Sex
{
get;
set;
}
double Score
{
get;
set;
}
}
class Graduate : ITeacher, IStudent
{
public Graduate(string n, int a, string t, string s, double sco, float w)
{
Name = n;
Age = a;
Title = t;
Sex = s;
Score = sco;
wage = w;
}
string name = " ";
public string Name
{
get { return name; }
set { name = value; }
}
int age = 0;
public int Age
{
get { return age; }
set { age = value; }
}
string title = " ";
public string Title
{
get { return title; }
set { title = value; }
}
string sex = " ";
public string Sex
{
get { return sex; }
set { sex = value; }
}
double score = 0.0;
public double Score
{
get { return score; }
set { score = value; }
}
//public void Set(double w)//可要可不要
//{
// wage = w;
//}
public void show()
{
Console.WriteLine(" 基本信息: ");
Console.WriteLine(" 姓名:{0}", name);
Console.WriteLine(" 性别:{0}", sex);
Console.WriteLine(" 年龄:{0}", age);
Console.WriteLine(" 职称:{0}", title);
Console.WriteLine(" 工资:{0}", wage);
Console.WriteLine(" 成绩:{0}", score);
}
private double wage;
}
class Program
{
static void Main()
{
Graduate grad = new Graduate("王莉", 24, "助教", "女", 89.5, 1234.50f);
grad.show();
}
}
//84,若程序设计语言不支持多态性,不能成为面向对象语言
using System;
class Unstudent
{
protected int no;
protected string name;
protected int fee1, fee2, fee3, fee4, fee;
public void calfee()
{
Console.Write(" 学号:");
no = Convert.ToInt32(Console.ReadLine());
Console.Write(" 姓名:");
name = Console.ReadLine();
fee1 = 4800;
fee2 = 800;
fee3 = 400;
fee4 = 200;
fee = fee1 + fee2 + fee3 + fee4;
}
public void disp()
{
Console.WriteLine(" 学 费:{0}",fee1);
Console.WriteLine( " 住宿费:{0}",fee2);
Console.WriteLine(" 书本费:{0}",fee3);
Console.WriteLine(" 其他费:{0}",fee4);
Console.WriteLine(" 总费用:{0}",fee);
}
public void fn()
{
calfee();
disp();
}
}
class Graduate : Unstudent
{
public new void calfee()
{
Console.Write(" 学号:");
no = Convert.ToInt32(Console.ReadLine());
Console.Write(" 姓名:");
name = Console.ReadLine();
fee1 = 1100;
fee2 = 600;
fee3 = 200;
fee = fee1 + fee2 + fee3;
}
public new void disp()
{
Console.WriteLine(" 住宿费:{0}", fee1);
Console.WriteLine(" 书本费:{0}", fee2);
Console.WriteLine(" 其他费:{0}", fee3);
Console.WriteLine(" 总费用:{0}", fee);
}
}
class Program
{
static void Main()
{
Unstudent s1 = new Unstudent();
Graduate s2 = new Graduate();
Console.WriteLine("本科生收费");
s1.fn();
Console.WriteLine("研究生收费");
s2.fn();
}
}
//85,虚方法就是允许被其他派生类重新定义的方法,在方法声明时需要使用virtual修饰符
//virtual修饰符不能与static,cabstract,override修饰符同时使用,并且虚方法是从基类继承来的公有成员,因此virtual修饰符也不能与private修饰符同时使用
using System;
class A
{
public void F()
{
Console.WriteLine("A.F");
}
public virtual void G()
{
Console.WriteLine("A.G");
}
}
class B : A
{
public new void F()
{
Console.WriteLine("B.F");
}
public override void G()
{
Console.WriteLine("B.G");
}
}
class Program
{
static void Main()
{
B b = new B();
A a = b;
a.F();
b.F();
a.G();
b.G();
}
}
/*86,重写方法,使用了override修饰符,表明该方法是重写方法,主要用来使用相同的签名重写继承的虚方法,虚方法主要用来引入新方法,而重写方法则使
从基类继承而来的虚方法专门化从而提供虚方法的具体实现。重写与重载是不同的,重写是指在派生类中重新写过基类的虚方法,而重载则是指编写一个与已有方法同名的但参数
列表不同的方法。另外,override修饰符不能与new static virtual修饰符同时使用,重写方法只能用于重写基类中的虚方法,不能用来单独声明方法*/
using System;
class Unstudent
{
protected int no;
protected string name;
protected int fee1, fee2, fee3, fee4, fee;
public void calfee()
{
Console.Write(" 学号:");
no = Convert.ToInt32(Console.ReadLine());
Console.Write(" 姓名:");
name = Console.ReadLine();
fee1 = 4800;
fee2 = 800;
fee3 = 400;
fee4 = 200;
fee = fee1 + fee2 + fee3 + fee4;
}
public void disp()
{
Console.WriteLine(" 学 费:{0}",fee1);
Console.WriteLine( " 住宿费:{0}",fee2);
Console.WriteLine(" 书本费:{0}",fee3);
Console.WriteLine(" 其他费:{0}",fee4);
Console.WriteLine(" 总费用:{0}",fee);
}
public virtual void fn()
{
calfee();
disp();
}
}
class Graduate : Unstudent
{
public new void calfee()
{
Console.Write(" 学号:");
no = Convert.ToInt32(Console.ReadLine());
Console.Write(" 姓名:");
name = Console.ReadLine();
fee1 = 1100;
fee2 = 600;
fee3 = 200;
fee = fee1 + fee2 + fee3;
}
public new void disp()
{
Console.WriteLine(" 住宿费:{0}", fee1);
Console.WriteLine(" 书本费:{0}", fee2);
Console.WriteLine(" 其他费:{0}", fee3);
Console.WriteLine(" 总费用:{0}", fee);
}
public override void fn()
{
calfee();
disp();
}
}
class Program
{
static void Main()
{
Unstudent s1 = new Unstudent();
Graduate s2 = new Graduate();
Console.WriteLine("本科生收费");
s1.fn();
Console.WriteLine("研究生收费");
s2.fn();
}
}
/*87,在C#中virtual修饰符必须与override修饰符配合使用,因此在派生类中fn()要显式表示,virtual修饰符放在基类中,指明该函数成员为虚方法(函数),
override修饰符放在派生类中,以体现它是重写方法(函数。用抽象类编写程序,分别求矩形面积和圆的面积)*/
using System;
public abstract class Shapes
{
public abstract void disp();
}
class Rectangle : Shapes
{
protected int x, y;
public void setvalue(int d, int w)
{
x = d;
y = w;
}
public override void disp()
{
Console.WriteLine("矩形面积:{0}", x * y);
}
}
class Circle : Shapes
{
protected int x;
public void setvalue(int d)
{
x = d;
}
public override void disp( )
{
Console.WriteLine("圆面积:{0}", 3.14159 * x * x);
}
}
class Program
{
static void Main()
{
Rectangle r1 = new Rectangle();
Circle c1 = new Circle();
r1.setvalue(10, 5);
r1.disp();
c1.setvalue(10);
c1.disp();
}
}
//88,委托是C#中的一种引用类型,通过委托实例调用静态方法和实例方法示例
using System;
delegate void D(int n);
class C
{
public static void M0(int i)
{
Console.WriteLine("调用静态方法M0!={0}", i);
}
public static void M1(int i)
{
Console.WriteLine("调用静态方法M1!={0}", i);
}
public void M2(int i)//无static
{
Console.WriteLine("调用静态方法M2!={0}", i);
}
}
class Program
{
static void Main()
{
D cd0 = new D(C.M0);
cd0(1);
D cd1 = new D(C.M1);
cd1(2);
C t = new C( );
D cd2 = new D(t.M2);
cd2(3);
D cd3 = new D(cd2);
cd3(4);
}
}