我们先看下面一段程序:
- /// <summary>
- /// 父类
- /// 作者:周公
- /// 首发地址:http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
- /// 日期:2008-09-01
- /// </summary>
- public class Father
- {
- public void Run0()
- {
- Console.WriteLine("Father.Run0");
- }
- }
- /// <summary>
- /// 子类
- /// 作者:周公
- /// 首发地址:http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
- /// 日期: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>
- /// 父类
- /// 作者:周公
- /// 首发地址:http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
- /// 日期:2008-09-01
- /// </summary>
- public class Father
- {
- public void Run1()
- {
- Console.WriteLine("Father.Run1");
- }
- }
- /// <summary>
- /// 子类
- /// 作者:周公
- /// 首发地址:http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
- /// 日期: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];