using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 父类隐藏方法 { class Program { class Animal { public void Show() { Console.WriteLine("This is Animal!");//父类 } public virtual void Type() //定义虚方法 {} } class dog : Animal { public new void Show()//为了防止在编译的过程中出现警告,我们需要在子类方法中用new来修饰要隐藏的方法 { base.Show(); Console.WriteLine("This is dog!"); //隐藏 } public void Shout() { Console.WriteLine("汪汪"); //扩展的方法 } public override void Type() //实现虚方法 { Console.WriteLine("类型:狗"); } } class bird:Animal { public void Show(string name) { Console.WriteLine("This is bird {0}",name);//重载 } public void Shout() { Console.WriteLine("唧唧"); //扩展的方法 } public override void Type() { Console.WriteLine("类型:鸟"); //实现虚方法 } } static void Main(string[] args) { Console.WriteLine("父类:"); Animal A=new Animal(); A.Show(); Console.WriteLine("隐藏:"); dog d = new dog(); d.Show(); d.Shout(); d.Type(); Console.WriteLine("重载:"); bird b=new bird(); b.Show("小麻雀"); b.Shout(); b.Type(); Console.WriteLine("类型转换:"); A = d; //将继承类转换为父类(隐式转换),调用的方法为父类的方法 A.Show(); Console.ReadLine(); } } } /*当方法在继承类中重新定义时,要在继承类中调用,要用关键字base*/ /*扩展(Extend):子类方法,父类没有; 重载(Overload):子类有父类的同名函数,但参数类型或数目不一样; 完全相同,隐藏(Hide):子类方法与父类方法从方法名称到参数类型完全一样。 */