在什么情况下从接口隐式结束显式实现相同方法是有意义的?
我知道差异,但我不知道为什么有时会使用它们?
interface I1
{
void A();
}
class B : I1
{
public void A()
{
Console.WriteLine("Implicit ");
}
void void I1.A()
{
Console.WriteLine("Explicit");
}
}
答案 0 :(得分:1)
您可以这样做,例如制作方法protected
并可通过界面访问。
通过这种方式,调用者只能通过接口声明访问它,或者它是从类派生的。无法从类本身或派生类访问显式接口成员。
class B : I1
{
protected void A()
{
Console.WriteLine("Implicit ");
}
void void I1.A()
{
Console.WriteLine("Explicit");
}
}
I1 i = new B();
i.A(); // works
B b = new B();
b.A(); // does not work