【问题标题】:call abstract function from a method's own implementation从方法自己的实现中调用抽象函数
【发布时间】:2014-03-10 16:08:24
【问题描述】:

我尝试从方法自己的实现中调用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 有“一些实现”,直到调用抽象函数。

【问题讨论】:

    标签: f# abstract


    【解决方案1】:

    您需要使用this 参考:

    this.doSomethingAbstract()
    

    【讨论】:

    • 啊,好的。谢谢。我认为只有 members 需要以这种方式引用。
    • abstract成员
    • 它是private,尚未使用abstract member 声明。这不是abstract 方法吗?
    • 不确定确切的定义,但我怀疑使用关键字member 定义的任何内容都是成员。这将包括属性和方法。
    • let 绑定不能使用 this 引用调用,只能使用成员。诚然,abstract 成员定义不包含关键字 member,但覆盖或默认实现包含。
    猜你喜欢
    • 1970-01-01
    • 2017-05-02
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 2013-08-25
    • 2015-12-22
    相关资源
    最近更新 更多