有人可以快速回答我吗...
在以下完全无用的代码中,在“类 DuplicateInterfaceClass :MyInterface1,MyInterface2”下。
为什么我不能显式写“public string MyInterface2.P()”?
然而“公共字符串 P()”和“字符串 MyInterface2.P()”工作。
我了解所有接口方法(属性等)默认情况下都是隐式“公共”的,但我在继承类中显式尝试导致“错误 CS0106:修饰符 'public' 对此项无效”。
using System;
interface MyInterface1
{
void DuplicateMethod();
// interface property
string P
{ get; }
}
interface MyInterface2
{
void DuplicateMethod();
// function ambiguous with MyInterface1's property
string P();
}
// must implement all inherited interface methods
class DuplicateInterfaceClass : MyInterface1, MyInterface2
{
public void DuplicateMethod()
{
Console.WriteLine("DuplicateInterfaceClass.DuplicateMethod");
}
// MyInterface1 property
string MyInterface1.P
{ get
{ return ("DuplicateInterfaceClass.P property"); }
}
// MyInterface2 method
// why? public string P()...and not public string MyInterface2.P()?
string MyInterface2.P()
{ return ("DuplicateInterfaceClass.P()"); }
}
class InterfaceTest
{
static void Main()
{
DuplicateInterfaceClass test = new DuplicateInterfaceClass();
test.DuplicateMethod();
MyInterface1 i1 = (MyInterface1)test;
Console.WriteLine(i1.P);
MyInterface2 i2 = (MyInterface2)test;
Console.WriteLine(i2.P());
}
}