c# - 抽象类型定义 f#

标签 c# inheritance interface f# abstract-class

我希望在接口(interface)或抽象类中定义几个类型,但未指定实现。 然后我想在另一个接口(interface)中继承这个接口(interface),这样我就可以在 interface2 中使用在第一个接口(interface)中定义的类型来指定我的方法。

例如:

type Interface1 =
      type MyType1
      type MyType2

type Interface2 =
      inherit Interface1
      abstract member method1 : MyType1*MyType2 -> int


Module MyModule =

我的想法是,我希望模块随后实现接口(interface) 2,因此它应该实现 MyType1 和 MyType2,以及方法 1。

我没有在签名文件中执行所有这些操作的原因是我希望能够在 C# 中也实现 type1 和 2,但要实现 Interface1。

谁能帮我解决这个问题?

最佳答案

我认为您真正想要的是使用泛型:

type Interface2<'T1,'T2> =
      abstract member method1 : 'T1*'T2 -> int

你不需要Interface1在这里。那么如果有 Type1Type2在 C# 中实现(或在 F# 中实现),您的 C# 类可以继承自 Interface2<Type1,Type2>一切就绪。

编辑:如果我没有正确理解你的评论,你想对 'T1 设置一些限制条件。和 'T2这样他们就实现了特定的接口(interface)。所有这些的通用名称( 'T1Type1 等等)开始让我感到困惑,所以我将使用特定名称作为示例。假设您有一个通用的 IKeyboard接口(interface)和通用 IMouse接口(interface),并且您希望库的用户为您的方法实现特定的键盘和鼠标类。换句话说,'T1上面的类型必须来自 IKeyboard , 和 'T2上面的类型必须来自 IMouse .在这种情况下,type constraints是你要找的:

type IKeyboard = class end
type IMouse = class end

type IInputDevices =
    abstract member getInput<'K,'M when 'K :> IKeyboard and 'M :> IMouse> : 'K*'M -> int

关于c# - 抽象类型定义 f#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43888294/

相关文章:

c++ - Qt - 从基类继承类

javascript - 无法在 typescript 中扩展angularjs Controller

c# - 覆盖子类中的 ReadOnly 属性以使其读/写(VB.NET 或 C#)

带有接口(interface)的 Java 编译错误

c# - 这个设计是个好主意吗——接口(interface)和抽象类

c# - 如何比较 2 个数据表并在第 3 个数据表中获取唯一记录?

c# - 以编程方式访问存储在 WFFM 报告中的数据

c# - ValueTuple 通过反射设置字段

php - 生成 PHP 接口(interface)

c# - 永远不会调用 XmlSerializer 中覆盖的反序列化