当我想将base
上载到适当的接口类型(即A
)以便可以对其调用doA()时,出现解析错误。我知道base
(http://cs.hubfs.net/topic/None/58670)有点特殊,但到目前为止,我仍无法找到解决此特定问题的方法。
有什么建议?
type A =
abstract member doA : unit -> string
type ConcreteA() =
interface A with
member this.doA() = "a"
type ExtA() =
inherit ConcreteA()
interface A with
override this.doA() = "ex" // + (base :> A).doA() -> parse error (unexpected symbol ':>' in expression)
((new ExtA()) :> A).doA() // output: ex
等效的工作C#:
public interface A
{
string doA();
}
public class ConcreteA : A {
public virtual string doA() { return "a"; }
}
public class ExtA : ConcreteA {
public override string doA() { return "ex" + base.doA(); }
}
new ExtA().doA(); // output: exa
最佳答案
这等效于您的C#:
type A =
abstract member doA : unit -> string
type ConcreteA() =
abstract doA : unit -> string
default this.doA() = "a"
interface A with
member this.doA() = this.doA()
type ExtA() =
inherit ConcreteA()
override this.doA() = "ex" + base.doA()
ExtA().doA() // output: exa
base
不能单独使用,只能用于成员访问(因此存在解析错误)。请参见Classes on MSDN下的“指定继承”。