【发布时间】:2019-06-20 18:50:09
【问题描述】:
代码:
class Program
{
class A
{
public void abc(int x)
{
Console.WriteLine("abc from A");
}
}
class B : A
{
public void abc(double x)
{
Console.WriteLine("abc from B");
}
}
static void Main(string[] args)
{
B b = new B();
b.abc(100);
Console.ReadLine();
}
}
从 B 类实例调用 abc() 方法时, 即使我们将整数数据(即 100)作为参数传递,为什么程序的执行还要使用 double 定义的方法参数? 请注意,在父类中已经为整数定义了一个方法。
为什么上面程序的输出是“abc from B”而不是“abc from A”。请多多指教。
【问题讨论】:
-
另外,如果您将子类中的签名更改为例如字符串,则会调用父方法。 100 是一个有效的双精度数,因此调用了隐藏在 child 中的函数。
标签: c#