class PRogram { static void Main(string[] args) { Father a = new Father(); Child b = new Child(); a.Identity(); b.Identity(); Father c = new Child(); c.Identity(); Console.ReadKey(); } } class Father { public int a = 5; public virtual void Identity() { a = 10; Console.WriteLine("我是父类"+a); } } class Child : Father { public new void Identity() { //base.Identity(); Console.WriteLine("我是子类"+a); } }运行结果证明:重写: 调用方法会根据引用指向的实际对象进行调用。隐藏方法会根据引用进行调用。同时重写的方法如果想先运行父类的方法再运行子类的方法的话可以通过base点出父类的方法。
新闻热点
疑难解答