里氏转化(向上转型)

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
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值