【发布时间】:2017-01-14 00:23:02
【问题描述】:
我正在阅读有关隐式或显式接口方法实现的内容,但我仍然不明白它是如何工作的以及有什么好处。
有代码:
interface InterfaceOne
{
void MethodOne();
}
class ClassOne : InterfaceOne
{
public void MethodOne()
{
Console.WriteLine("hello from the class method");
}
void InterfaceOne.MethodOne()
{
Console.WriteLine("hello from the interface method");
}
}
以及来自 main 方法的代码:
var c1 = new ClassOne();
c1.MethodOne();
InterfaceOne i1 = new ClassOne();
i1.MethodOne();
Console.ReadLine();
这是输出:
来自类方法的你好
来自接口方法的你好
我的问题:
为什么我没有错误的类具有两个具有相同名称和签名的方法?
当我使用var关键字时,编译器如何选择调用哪个方法?
有什么好处?
【问题讨论】:
-
@MarcinJuraszek 它不是重复的,因为你提到的问题没有回答我的第二个问题