我有一个问题,我有两种类型,区别仅在于方法的签名。我的“ Plan-B”是忘记了这种差异,我将在具体的实现中进行处理,但是对于“ Plan-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;
}
换句话说,接口
A
将方法void MyMethod()
添加到接口MyInterface
,接口B
将方法ICollection<int> MyMethod()
添加到接口MyInterface
。请注意,您仍然无法拨打例如
MyInterface object = new AClass();
object.MyMethod();
因为
MyMethod()
不会是MyInterface
的成员。关于c# - 自定义界面实现和界面C#,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8022044/