这是这里的工作示例:
type MethodExample() =
// standalone method
member this.AddOne x =
x + 1
// calls another method
member this.AddTwo x =
this.AddOne x |> this.AddOne
Run Code Online (Sandbox Code Playgroud)
这就是我想要做的:
type MethodExample() =
// standalone method
member this.AddOne x =
x + 1
// calls another method
member this.AddTwo x =
this.AddOne x |> this.AddOne
Run Code Online (Sandbox Code Playgroud)
该AddOne功能不可用,我也尝试将其向下转换为 MethodExample,但它很糟糕并且不起作用。
我该怎么做?
在 F# 中,所有接口实现都是私有的——这意味着接口方法不像在 C# 中那样也作为类的公共方法出现。它们只能通过界面访问。
因此,为了访问它们,您需要先将您的类转换为接口:
member this.AddTwo x =
let me = this :> IMethod
me.AddOne x |> me.AddOne
Run Code Online (Sandbox Code Playgroud)