没有参数列表的类定义

dha*_*ech 5 f#

F#接受以下内容:

type Abc =
    member this.A = 10
Run Code Online (Sandbox Code Playgroud)

由于没有提供参数列表,因此没有默认构造函数.可以添加构造函数Abc吗?如果没有,可以做些Abc什么?

Dan*_*iel 5

我想不出很多用途,但你可以做的两件事情

  1. 从它继承,虽然派生类型也不能实例化
  2. 使用静态成员扩展它(尽管使用模块可以更好地实现,也可以扩展)
type Abc =
    member this.A = 10

[<Class>]
type Def = 
    inherit Abc

type Abc with
    static member Foo() = ()
Run Code Online (Sandbox Code Playgroud)

在C#中,接口或抽象类的代码约定在"契约类"中定义,它必须标记为抽象并具有私有构造函数(即它是不可实例化的).在我看来,这是实现这一目标的更好方法.但是C#不支持无构造函数的类.