.NET 继承 多态 封装 面试题

时间:2021-10-13 21:58:16

 DOOSAN面试题目其一:

 

    public class A
    {
        public virtual void Fun1(int i)
        {
            Console.WriteLine(i);
        }

        public void Fun2(A a)
        {
            a.Fun1(1);
            Fun1(5);
        }

   
    }

    public class B : A
    {
        public override void Fun1(int i)
        {
            base.Fun1(i + 1);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            B b = new B();
            A a = new A();

            a.Fun2(b);
            b.Fun2(a);
          
            Console.ReadKey();

        }
    }

-----------------

结果分别为什么呢?2,5,1,6

若上述的红色override修改为new呢? 1,5,1,5

 

具体可以参考这里的理论学习http://blog.csdn.net/cc_net/article/details/2955771