override new

class Base  
{  
    public virtual void F1()  
    {  
        Console.WriteLine("Base's virtual function F1");  
    }  
    public virtual void F2()  
    {  
        Console.WriteLine("Base's virtual fucntion F2");  
    }  
}  
class Derived:Base  
{  
    public override void F1()  
    {  
        Console.WriteLine("Derived's override function F1");  
    }  
    public new void F2()  
    {  
        Console.WriteLine("Derived's new function F2");  
    }  
}  
class Program  
{  
    public static void Main(string[] args)  
    {  
        Base b1 = new Derived();  
        //由于子类覆盖了父类的方法,因此这里调用的是子类的F1方法。也是OO中多态的体现  
        b1.F1();  
        //由于在子类中用new隐藏了父类的方法,因此这里是调用了隐藏的父类方法  
        b1.F2();   

    b1 = new Derived();
    //由于子类覆盖了父类的方法,因此这里调用的是子类的F1方法。也是OO中多态的体现
    ((Base) b1).F1();
    //由于在子类中用new隐藏了父类的方法,因此这里是调用了隐藏的父类方法
    ((Base) b1).F2();
    }  
}  

 

posted on 2018-03-11 16:00  强博员  阅读(90)  评论(0)    收藏  举报

导航