我有一个问题,我有两种类型,区别仅在于方法的签名。我的“B 计划”是忘记这种差异,我在具体实现中处理它,但对于“A 计划”我想要以下内容:
public interface MyInterface
{
int property;
void MyMethod();
ICollection<int> MyMethod();
}
public interface A : MyInterface
{
void MyMethod();
}
public class AClass : A
{
public void MyMethod() { ... }
}
public interface B : MyInterface
{
ICollection<int> MyMethod();
}
public class BClass : B
{
public ICollection<int> MyMethod() { ... }
}
所以我希望 AClass 必须实现 A 的方法,而不必实现 MyInterface 的 ICollection MyMethod。
希望我写得清楚。
可能吗?
谢谢!
最佳答案
不确定我是否理解准确,但试试这个:
public interface MyInterface
{
int property;
}
public interface A : MyInterface
{
void MyMethod();
}
public class AClass : A
{
public void MyMethod() { ... }
int property;
}
public interface B : MyInterface
{
ICollection<int> MyMethod();
}
public class BClass : B
{
public ICollection<int> MyMethod() { ... }
int property;
}
换句话说,接口(interface)A
添加方法void MyMethod()
至接口(interface)MyInterface
,和接口(interface)B
添加方法ICollection<int> MyMethod()
至接口(interface)MyInterface
.
请注意,您仍然无法调用电话,例如
MyInterface object = new AClass();
object.MyMethod();
如MyMethod()
不会成为 MyInterface
的成员.
关于c# - 自定义接口(interface)实现和接口(interface)c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8022044/