gpt4 book ai didi

c# - 自定义接口(interface)实现和接口(interface) c#

转载 作者:行者123 更新时间:2023-11-30 22:34:02 24 4
gpt4 key购买 nike

我有两个类型的问题,区别仅在于方法的签名。我的“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;
}

换句话说,接口(interface)A添加方法 void MyMethod()到界面 MyInterface和界面 B添加方法 ICollection<int> MyMethod()到界面 MyInterface .

请注意,您仍然无法调用 e.g.

MyInterface object = new AClass();
object.MyMethod();

作为MyMethod()不会成为 MyInterface 的成员.

关于c# - 自定义接口(interface)实现和接口(interface) c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8022044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com