首页 »DotNet » override:new、virtual、override基础实例学习 »正文
override:new、virtual、override基础实例学习
来源: 发布时间:星期三, 2008年9月10日 浏览:110次 评论:0
我们先看下面一段程序:
/// <summary> /// 父类 /// 作者:周公 /// 日期:2008-09-01 /// </summary> public class Father { public void Run0() { Console.WriteLine("Father.Run0"); } }
/// <summary> /// 子类 /// 作者:周公 /// 日期:2008-09-01 /// </summary> public class Son:Father { public void Run0() { Console.WriteLine("Son.Run0"); } } class Program { static void Main(string[] args) { Father[] fatherList = new Father[2]; fatherList[0] = new Father(); fatherList[1] = new Son();
fatherList[0].Run0(); fatherList[1].Run0(); } }
程序的运行结果是: Father.Run0
Father.Run0
稍微细心的朋友可能发现在Son类的Run0方法下面有一段棕色的波浪线,当我们把鼠标放到该下划线上时,会看到下面的提示(编译程序时在程序的“输出”窗口也能看到这个警告):
“MethodDemo.Son.Run0()”隐藏了继承的成员“MethodDemo.Father.Run0()”。如果是有意隐藏,请使用关键字new。
如图:
然后我们再来第二个版本的Run方法,我们称之为Run1(),,与第一个版本的区别是在子类的同名同参(方法名相同,参数个数和参数顺序相同,下同)方法前面加上了一个new关键字,代码如下:
/// <summary> /// 父类 /// 作者:周公 /// 日期:2008-09-01 /// </summary> public class Father { public void Run1() { Console.WriteLine("Father.Run1"); } }
/// <summary> /// 子类 /// 作者:周公 /// 日期:2008-09-01 /// </summary> public class Son:Father { public new void Run1() { Console.WriteLine("Son.Run1"); } }
class Program { static void Main(string[] args) { Father[] fatherList = new Father[2]; fatherList[0] = new Father(); fatherList[1] = new Son();
fatherList[0].Run1(); fatherList[1].Run1(); } } 运行结果如下:
Father.Run1
Father.Run1
我们发现加上new关键字之后,程序的运行结果没有发生改变。也就是在C#中,如果在子类中有与父类同名同参的方法时,C#会隐式帮你在子类的方法前面添加一个new关键字。
我们再写第三个版本的Run方法,即Run2(),与第一个版本不同的是父类的Run2()方面前面加了一个virtual关键字,表示这是一个虚方法,子类的Run2()除了与第一个版本号不同之外(Run0()改成Run2())没有什么不同。
程序代码如下:
/// <summary> /// 父类 /// 作者:周公 /// 日期:2008-09-01 /// </summary> public class Father { public virtual void Run2() { Console.WriteLine("Father.Run2"); } }
/// <summary> /// 子类 /// 作者:周公 /// 日期:2008-09-01 /// </summary> public class Son:Father { public void Run2() { Console.WriteLine("Son.Run2"); } } class Program { static void Main(string[] args) { Father[] fatherList = new Father[2]; fatherList[0] = new Father(); fatherList[1] = new Son();
fatherList[0].Run2(); fatherList[1].Run2(); } } 我们看看程序的运行效果:
Father.Run2
Father.Run2
程序运行效果与第一个仍然没有什么区别,不过这次子类(Son)的Run2()方法又出现了与Run方法的第一个版本(Run0())一样的警告:“MethodDemo.Son.Run2()”将隐藏继承的成员“MethodDemo.Father.Run2()”。若要使当前成员重写该实现,请添加关键字override。否则,添加关键字new。
我们再写第四个版本的Run方法,我们称之为Run3(),这次父类中Run3()的修饰符与第三个版本相比没有变化(依然是virtual,虚方法),而子类Son中的Run3()方法与第三个版本相比多了一个override修饰符(这次我们熟悉的那个棕色的波浪线警告没有了)。代码如下:
/// <summary> /// 父类 /// 作者:周公
/// 日期:2008-09-01 /// </summary> public class Father { public virtual void Run3() { Console.WriteLine("Father.Run3"); } }
/// <summary> /// 子类 /// 作者:周公 /// 首发地址:http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx /// 日期:2008-09-01 /// </summary> public class Son:Father { public override void Run3() { Console.WriteLine("Son.Run3"); } } class Program { static void Main(string[] args) { Father[] fatherList = new Father[2]; fatherList[0] = new Father(); fatherList[1] = new Son();
fatherList[0].Run3(); fatherList[1].Run3(); } } 程序的运行结果如下:
Father.Run3
Son.Run3
相关文章
读者评论
发表评论
|
|