class SecondExp 
{ public int firstMethod() 
{ return(secondMethod()*thirdMethod()); } public int secondMethod() 
{ return(10); } public int thirdMethod() 
{ return(20); } } class DerivedClass : SecondExp 

public new int secondMethod() 
{ return (30); } 

class Test 

public static void Main() 
{ //第一种:
SecondExp objDerived = new DerivedClass(); 
objDerived.secondMethod(); //注意这种方法
//第二种:
DerivedClass objDerived = new DerivedClass(); 
objDerived.secondMethod(); //注意这种方法System.Console.WriteLine(objDerived.firstMethod()); 


解决方案 »

  1.   

    第一种情况下
    父类的引用(SecondExp objDerived)指向一个子类的对象(new DerivedClass()) 而且方法secondMethod()在子类中使用new进行声明 所以使用的规则是"谁的声明就用谁的方法" 也就是说使用的是父类的secondMethod方法
    同理分析 第二种情况下 
    使用的是子类的secondMethod方法
      

  2.   

    如果在子类中对父类的方法进行了"override"声明
    则不管是SecondExp objDerived = new DerivedClass()
    还是DerivedClass objDerived = new DerivedClass() 
    调用的都将是子类的已经重写的secondMethod()
      

  3.   

    在基类中virtual、abstract或override的方法
    在派生类override以后,才有多态
    如果使用new关键字,是隐藏,基类的方法与派生类的方法是毫无关系的两个方法
    用类型为基类的对象,调用的是基类的方法
      

  4.   

    第一种情况下 
    父类的引用(SecondExp objDerived)指向一个子类的对象(new DerivedClass()) 而且方法secondMethod()在子类中使用new进行声明 所以使用的规则是"谁的声明就用谁的方法" 也就是说使用的是父类的secondMethod方法 
    同理分析 第二种情况下 
    使用的是子类的secondMethod方法
    我子类中如果没有NEW的话还是调用的父类的方法
      

  5.   

    public new int secondMethod()
    -------
    既然使用new覆盖说明你知道这样做的结果...既然知道结果说明你愿意接受这种方式...既然是你自愿的...还有什么疑问?
      

  6.   

    支持五楼,使用new都知道结果了
      

  7.   

    我也并不是在说你子类中如果不使用new时候的情况啊 呵呵~new是在子类对象中对父类成员进行覆盖的显示声明(换句话说你写不写它 本质上没有区别 它只是个"显示的"声明)
    "我子类中如果没有NEW的话还是调用的父类的方法" 
    只能说明你使用的是SecondExp objDerived = new DerivedClass()中的objDerived(一个声明为父类的对象)来调用的方法只要你不是"override" 就遵循"谁的声明就用谁的方法"