这是here中的有效示例:
type MethodExample() =
// standalone method
member this.AddOne x =
x + 1
// calls another method
member this.AddTwo x =
this.AddOne x |> this.AddOne
这就是我想要做的:
type IMethod =
abstract member AddOne: a:int -> int
abstract member AddTwo: a:int -> int
type MethodExample() =
interface IMethod with
member this.AddOne x =
x + 1
// calls another method
member this.AddTwo x =
this.AddOne x |> this.AddOne
AddOne
功能不可用,我也尝试将其向下转换为MethodExample,但它很可怕,无法正常工作。
我该怎么办?
答案 0 :(得分:2)
在F#中,所有接口实现都是私有的-意味着接口方法也不会像C#中那样作为类的公共方法出现。它们只能通过界面访问。
因此,为了访问它们,您需要首先将类强制转换为接口:
member this.AddTwo x =
let me = this :> IMethod
me.AddOne x |> me.AddOne