我试图从方法自己的实现中调用abstract函数。
[<AbstractClass>]
type CallAnAbstract () =
let doSomethingElse () = printfn "Done Something.."
abstract doSomethingAbstract: unit -> unit
member this.DoSomething =
printfn "Done Something.."
doSomethingElse ()
doSomethingAbstract ()
导致错误:
错误FS0039:值或构造函数'doSomethingAbstract'不是 定义
从方法自己的实现调用抽象函数是非法的还是问题出在其他地方?
如果是非法的话, here(见答案)@Mark Seemann解释了继承的构成:
[<AbstractClass>]
type UseComposition (doSomethingElseDefinedLater) =
let doSomethingElse () = printfn "Done Something.."
member this.DoSomething =
printfn "Done Something.."
doSomethingElse ()
doSomethingElseDefinedLater ()
有哪些替代方案?我避免通过构造函数传递函数,我想重用一些代码,让DoSomething
具有“一些实现”,直到调用抽象函数。
答案 0 :(得分:5)
您需要使用this
引用:
this.doSomethingAbstract()