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

标签 c# .net

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

相关文章:

c# - 在 C# 中从资源/程序集加载 excel 文件

c# - 删除 4 字节的 UTF8 字符

c# - 无法通过 sdk 从 cosmos db 中删除特定文档?

c# - Visual Studio 2015 'use var' 重构快捷方式

c# - 如何从照片中获取任意颜色的 channel ?

.net - 在 .NET 中创建的 Guid 的前四个字节的分布有多均匀?

c# - 如何在sql中将带有时区的varchar转换为datetime

c# - 汇编版本所需的格式是什么?

.net - 非 UI 线程可以显示在屏幕上吗?

c# - 有没有类似 Android RecyclerView for C# Winforms 的东西