F#提供接口类型。它提供了纯抽象。它是抽象方法的集合。实现接口的类必须为其所有方法提供定义。
句法:
// Interface declaration:
[ attributes ]
type interface-name =
[ interface ] [ inherit base-interface-name ...]
abstract member1 : [ argument-types1 -> ] return-type1
abstract member2 : [ argument-types2 -> ] return-type2
...
[ end ]
F#接口示例
在此示例中, 我们创建了一个接口并将其实现为一个类。
type IEmployee =
abstract member ShowName : unit -> unit
type Manager(id:int, name:string) =
interface IEmployee with
member this.ShowName() = printfn "Id = %d \nName = %s" id name
let manager = new Manager(100, "RajKumar")
//manager.ShowName() // error: you can't directly access interface abstract method by using class object.
// so, we need to upcast class type object to interface type by using :> operator.
(manager :> IEmployee).ShowName()
输出:
Id = 100
Name = RajKumar
评论前必须登录!
注册