public string Meth3() 需要override 修饰!

解决方案 »

  1.   

    new 修饰符   用于隐藏基类成员的继承成员
    在你添加037行后就把基类的同名函数给暴露的!
    new只是隐藏,override才是重载!
      

  2.   

    因为你的MATH2和MATH3不是重载而是重新定义
      

  3.   

    我知道027行系统在这里将会有一个警告。我对以下不是很懂。036: MyDerived mD = new MyDerived();
    037: MyBase mB = (MyBase) mD;
    038: 
    039: System.Console.WriteLine(mB.Meth1());
    040: System.Console.WriteLine(mB.Meth2());
    041: System.Console.WriteLine(mB.Meth3());
    042: }
    043: }输出:MyDerived-Meth1
    MyBase-Meth2
    MyBase-Meth3如果你把第037行去掉,把039-041中的mB全部改为mD,输出又变为:MyDerived-Meth1
    MyDerived-Meth2
    MyDerived-Meth3037行如何理解?
    039,040,041输出的结果如何解释??
      

  4.   

    MyDerived  中Meth1是重写MyBase 中的Meth1
    MyDerived  中Meth2是覆盖MyBase 中的Meth2
    MyDerived  中Meth3是错误的写法。
    MyDerived mD = new MyDerived();  //MyDerived  的实例
    037: MyBase mB = (MyBase) mD;  如何理解?
      

  5.   

    MyBase mB = (MyBase) mD
    mD已经转变为它的父类
    mB引用了mD(当然已经转型了的)
    mD重写了父类MyBase的Meth1
    其他的只是覆盖 隐藏(new 关键字)mB是MyBase类型
    040: System.Console.WriteLine(mB.Meth2());
    041: System.Console.WriteLine(mB.Meth3());
    调用的是MyBase类型的方法039: System.Console.WriteLine(mB.Meth1());
    调用的是MyDerived中的方法Meth1()
    因为mB是从mD显式转换过来的 mD里重写了Meth1(),
    除非用Base.Meth1(),才会调用MyBase里的Meth1()。
      

  6.   

    MyBase mB = (MyBase) mD; 
    是对mD的引用.mB并没有分配内存.
      

  7.   

    楼上所说极是...
    用下面的方法可以验证:MyDerived mD2 = mB as MyDerived;
    Debug.Assert(mD2 != null);如果.mB重新分配了内存,构造了myBase对象,那么这里将出现断言错误...
      

  8.   

    ofei(欧卡) 的见解独特。还有没有人有不同的看法
      

  9.   

    mb实际上还是一个你的DERIVED 对象。就是这个原因吧。