今天网易学院看到一篇介绍c#中多态性的文章,作者满嘴漏风的说:使用override来重载基类中的virtual函数,FT,看来这个老大是知道怎么回事,但是用词错误了,假如让新手看到,不把人家搞晕才怪呢。
重载Is overload not override
在C#中,所谓的重载就是同一个类中不同签名的同名方法。当然调用时编译器会根据实参列表自动选择匹配形参列表的方法。 上面的那篇文章应该是指覆写。
覆写 is override
覆写这个概念只存在于继承关系的两个类中,基类的方法是virtual关键字标示的虚方法,子类是该虚方法的同名方法,使用了override关键字进行了覆写。
覆写override的关键意义在于,在使用父类的对象引用来引用子类的对象时,调用该同名方法,编译器会自动调用子类中的方法,而不是调用父类的方法。
覆盖 is new
假如父类中的方法并不是virtual的,子类中的方法也就无法使用override关键字进行覆写,而使用new关键字对父类中的方法进行覆盖,覆盖后,父类的对象引用来引用子类的对象时,进行调用该同名方法,会调用父类的方法,虽然实际上引用的是子类的对象。
看懂我下面的代码 我觉得你就理解了
下面是示例代码:
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Hello
{
interface IHello
{
void sayHello();
}
class Hello : IHello
{
public virtual void sayHello()
{
Console.WriteLine("Hello!");
}
}
class HelloLee : Hello
{
public override void sayHello()
{
Console.WriteLine("Hello Lee!");
}
}
class HelloCoderLee : HelloLee
{
public new void sayHello()
{
Console.WriteLine("Hello CoderLee!");
}
}
class Program
{
static void Main(string[] args)
{
IHello sayHello;
sayHello = new HelloCoderLee();
sayHello.sayHello();
Console.ReadLine();
}
}
}
重载Is overload not override
在C#中,所谓的重载就是同一个类中不同签名的同名方法。当然调用时编译器会根据实参列表自动选择匹配形参列表的方法。 上面的那篇文章应该是指覆写。
覆写 is override
覆写这个概念只存在于继承关系的两个类中,基类的方法是virtual关键字标示的虚方法,子类是该虚方法的同名方法,使用了override关键字进行了覆写。
覆写override的关键意义在于,在使用父类的对象引用来引用子类的对象时,调用该同名方法,编译器会自动调用子类中的方法,而不是调用父类的方法。
覆盖 is new
假如父类中的方法并不是virtual的,子类中的方法也就无法使用override关键字进行覆写,而使用new关键字对父类中的方法进行覆盖,覆盖后,父类的对象引用来引用子类的对象时,进行调用该同名方法,会调用父类的方法,虽然实际上引用的是子类的对象。
看懂我下面的代码 我觉得你就理解了
下面是示例代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)