Gal*_*hen 12 c# inheritance overloading
我想知道调用打印"double in derived"的方法的原因是什么.我没有在C#规范中找到任何线索.
public class A
{
public virtual void Print(int x)
{
Console.WriteLine("int in base");
}
}
public class B : A
{
public override void Print(int x)
{
Console.WriteLine("int in derived");
}
public void Print(double x)
{
Console.WriteLine("double in derived");
}
}
B bb = new B();
bb.Print(2);
Run Code Online (Sandbox Code Playgroud)
直接来自C#规范(7.5.3过载分辨率):
方法调用的候选集不包括标记为override的方法(第7.4节),如果派生类中的任何方法适用(第7.6.5.1 节),则基类中的方法不是候选方法.
在您的示例中,overriden Print(int x)不是候选者并且Print(double x)是适用的,因此在不需要考虑基类中的方法的情况下进行选择.
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |