- F# 基础教程
- F# - 主页
- F# - 概述
- F# - 环境设置
- F# - 程序结构
- F# - 基本语法
- F# - 数据类型
- F# - 变量
- F# - 运算符
- F# - 决策
- F# - 循环
- F# - 函数
- F# - 字符串
- F# - 选项
- F# - 元组
- F# - 记录
- F# - 列表
- F# - 序列
- F# - 集
- F# - 地图
- F# - 受歧视的工会
- F# - 可变数据
- F# - 数组
- F# - 可变列表
- F# - 可变字典
- F# - 基本 I/O
- F# - 泛型
- F# - 代表
- F# - 枚举
- F# - 模式匹配
- F# - 异常处理
- F# - 类
- F# - 结构
- F# - 运算符重载
- F# - 继承
- F# - 接口
- F# - 事件
- F# - 模块
- F# - 命名空间
- F# 有用资源
- F# - 快速指南
- F# - 有用的资源
- F# - 讨论
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 ] // Implementing, inside a class type definition: interface interface-name with member self-identifier.member1 argument-list = method-body1 member self-identifier.member2 argument-list = method-body2 // Implementing, by using an object expression: [ attributes ] let class-name (argument-list) = { new interface-name with member self-identifier.member1 argument-list = method-body1 member self-identifier.member2 argument-list = method-body2 [ base-interface-definitions ] } member-list
请注意 -
在接口声明中,成员未实现。
成员是抽象的,由abstract关键字声明。但是,您可以使用default关键字提供默认实现。
您可以通过使用对象表达式或使用类类型来实现接口。
在类或对象实现中,需要为接口的抽象方法提供方法体。
关键字interface和end标记定义的开始和结束,是可选的。
例如,
type IPerson = abstract Name : string abstract Enter : unit -> unit abstract Leave : unit -> unit
调用接口方法
接口方法是通过接口调用的,而不是通过实现接口的类或类型的实例。要调用接口方法,可以使用:>运算符(向上转换运算符)向上转换为接口类型。
例如,
(s :> IPerson).Enter() (s :> IPerson).Leave()
下面的例子说明了这个概念 -
例子
type IPerson = abstract Name : string abstract Enter : unit -> unit abstract Leave : unit -> unit type Student(name : string, id : int) = member this.ID = id interface IPerson with member this.Name = name member this.Enter() = printfn "Student entering premises!" member this.Leave() = printfn "Student leaving premises!" type StuffMember(name : string, id : int, salary : float) = let mutable _salary = salary member this.Salary with get() = _salary and set(value) = _salary <- value interface IPerson with member this.Name = name member this.Enter() = printfn "Stuff member entering premises!" member this.Leave() = printfn "Stuff member leaving premises!" let s = new Student("Zara", 1234) let st = new StuffMember("Rohit", 34, 50000.0) (s :> IPerson).Enter() (s :> IPerson).Leave() (st :> IPerson).Enter() (st :> IPerson).Leave()
当您编译并执行该程序时,它会产生以下输出 -
Student entering premises! Student leaving premises! Stuff member entering premises! Stuff member leaving premises!
接口继承
接口可以继承一个或多个基接口。
下面的例子展示了这个概念 -
type Interface1 = abstract member doubleIt: int -> int type Interface2 = abstract member tripleIt: int -> int type Interface3 = inherit Interface1 inherit Interface2 abstract member printIt: int -> string type multiplierClass() = interface Interface3 with member this.doubleIt(a) = 2 * a member this.tripleIt(a) = 3 * a member this.printIt(a) = a.ToString() let ml = multiplierClass() printfn "%d" ((ml:>Interface3).doubleIt(5)) printfn "%d" ((ml:>Interface3).tripleIt(5)) printfn "%s" ((ml:>Interface3).printIt(5))
当您编译并执行该程序时,它会产生以下输出 -
10 15 5