c# - 为什么编译器在不进一步继承的情况下自动转换?

标签 c# .net wpf oop inheritance

当我尝试运行以下代码片段时,它执行了错误的重载方法。我很困惑为什么要这样做? [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/

相关文章:

c# - 我需要保留对 FileSystemWatcher 的引用吗?

c# - 在 C# 中将任意类型实例写入 MemoryStream

c# - 如何正确监控 MassTransit Courier 路由单?

c# - 带 OR 的 linq where 语句

.net - signalR 中何时会发生重新连接?

wpf - 在 Josh Smith 的文章之后,在 MVVM 的 ListView 中添加 ComboBox

wpf - 是否可以将自定义拼写检查字典添加到样式中?

c# - WPF 数据绑定(bind) : enable/disable a control based on content of var?

c# - 如何通过代码隐藏从第三方获取用户的客户端IP

.net - AddressFamily.FireFox 在 .NET 套接字中指的是什么?