F# 静态覆盖

F# static override

我有一个抽象 class,有一个抽象成员。 我想将该成员继承给不同的 classes 并使成员的覆盖成为静态的。

像这样:

[<AbstractClass>]
type Parent() = class
  abstract member Att : int
end;;
type Son() = class
  inherit Parent()
  static override Att = 10
end;;
type Daughter() = class
  inherit Parent()
  static override Att = 20
end;;

或者

[<AbstractClass>]
type Parent() = class
  static abstract member Att : int
end;;

[<AbstractClass>]
type Parent() = class
  abstract static member Att : int
end;;

那么所有儿子的 Att = 10,所有女儿的 Att = 20。 这不起作用。

有没有办法让它工作?

根据对象模型的定义,这是不可能的 - 在 C# 和任何其他对象语言中也不能覆盖静态方法。

例如这里也是(对于 Java,但这对 object-oriented 编程来说是通用的):

Why cannot we override static method in the derived class

Overriding depends the an instance of a class. Polymorphism is that you can subclass a class and the objects implementing those subclasses will have different behaviors for those method defined in the superclass (and overridden in the subclasses). Static methods do not belong to an instance of a class so the concept is not applicable.

特别是对于抽象 class,这没有意义 - 您要么在派生 class 上调用方法(这意味着它不需要在 superclass),或者在 superclass 上(这意味着它不需要是抽象的),或者在一个实例上(这意味着它不能是静态的)。