static void Main(string[] args)
{
Chinese c = new Chinese();
c.Speak();//普通话
c.Eat();//中餐
ShenzhenRen s = new ShenzhenRen();
s.Speak();//粤语
s.Eat();//粤菜
//向上转型:
//ch只能访问shenzhenren继承Chinese成员
//定义父类的类型变量存储子类的对象
Chinese ch = new ShenzhenRen();
//继承eat(),自定义eat(),speak()
ch.Eat();//中餐,优先调用继承的
ch.Speak();//粤语,继承被覆盖就调用子类重写的;
}
2.class A {
public virtual String show(D obj)...{
return ("A and D");
}
public virtual String show(A obj)...{
return ("A and A");
}
}
class B : A{
public overide String show(B obj)...{
return ("B and B");
}
public new String show(A obj)...{
return ("B and A");
}
}
class C : B...{}
class D : B...{}
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
Console.WriteLine(a1.show(b)); ①A and A
Console.WriteLine(a1.show(c)); ②A and A
Console.WriteLine(a1.show(d)); ③A and D
Console.WriteLine(a2.show(b)); ④A and A
Console.WriteLine(a2.show(c)); ⑤A and A
Console.WriteLine(a2.show(d)); ⑥A and D
Console.WriteLine(b.show(b)); ⑦//B and B
Console.WriteLine(b.show(c)); ⑧//B and B
Console.WriteLine(b.show(d)); ⑨//B and B
3.abstract calss AA{
public abstract string show1();
}
abstract class A:AA{
public abstract string show();
}
class B:A{
public overide string show(){
return "BB";
}
public overide string show1(){
return "BA";
}
}
class C:A{
public overide string show(){
return "CC";
}
public overide string show1(){
return "CA";
}
}
class D:C{
public overide string show(){
return "DD";
}
public new string show1(){
return "DA";
}
}
public static void Main(){
C c=new C();
Console.WriteLine(c.show()); //CC
Console.WriteLine(c.show1());CA
A a=new C();
Console.WriteLine(a.show());CC
Console.WriteLine(a.show1());CA
D d=new D();
Console.WriteLine(d.show());DD
Console.WriteLine(d.show1());DA
C f=new D();
Console.WriteLine(f.show());//DD
Console.WriteLine(f.show1());//CA
}
里氏转化(向上转型)
最新推荐文章于 2021-04-05 16:26:23 发布