考慮兩個接口:如何通過保留方法名稱來擴展接口?
interface I1 {
int Foo();
}
interface I2 {
void Foo();
}
和A類:
class Test : I1, I2 {
int I1.Foo() {
Console.WriteLine("I1.Foo");
return default(int);
}
public void Foo() {
Console.WriteLine("I2.Foo");
}
}
我如何通過保持命名Foo
的方法擴展與I1
接口I2
?
我試着下面的代碼,但它不會編譯:
interface I1 {
int Foo();
}
interface I2 : I1 {
void I2.Foo();
}
class Test : I2 { /* same code */ }
你不能在接口中實現* anything *。只允許聲明。 –
這不是非常有用,但肯定是允許的。您將希望避免使用相同的成員名稱,這使得類難以實現該界面並讓任何人使用它。 –