首页 > 其他 > 详细

面试题002(继承&重写)

时间:2020-12-22 11:54:52      阅读:20      评论:0      收藏:0      [点我收藏+]

 public class Test2A
    {
        int i = 1;
        public virtual void M1()
        {
            Console.WriteLine("i am A");
        }
        public void M2()
        {
            Console.WriteLine("i = {0}",i);
        }
    }

    public class Test2B : Test2A
    {
        int i = 2;
        public override void M1()
        {
            Console.WriteLine("i am b");
        }

        public void M2()
        {
            Console.WriteLine("i = {0}", i);
        }
    }

 

   Test2B b = new Test2B();
   Test2A a = b;
   a.M1();   //M1被重写了,b的M1方法
   a.M2();  //调用为a的M2方法 
   b.M1();   
   b.M2();

 

面试题002(继承&重写)

原文:https://www.cnblogs.com/sportdog/p/14172000.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!