当我尝试运行以下代码片段时,它执行了错误的重载方法。我很困惑为什么要这样做? [testB.TestMethod(testValue)
方法执行public double TestMethod(double value)
方法]
public class TestA
{
public int TestMethod(int value)
{
return value;
}
}
public class TestB : TestA
{
public double TestMethod(double value)
{
return value;
}
}
static void Main( string[] args )
{
TestB testB = new TestB();
int testValue = 3;
testB.TestMethod(testValue);
}
你有什么想法吗?
有没有什么方法可以通过 TestB 实例调用 TestA 类方法而不用强制转换为 TestA。
但是在JAVA和C++中就不会发生
最佳答案
根据规范,在“过载解决方案”下:
...and methods in a base class are not candidates if any method in a derived class is applicable (§7.6.5.1).
关于c# - 为什么编译器在不进一步继承的情况下自动转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19112537/