F#在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
函数式 赛车模拟 (自由格式) 发布于:2021-02-02 10:52 转换OCaml对象模式 发布于:2021-02-01 00:46 [更多]
显示目录

接口



学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

接口

接口提供了一种抽象的方式来描述类的实现细节。 它是一个模板,声明类必须实现和公开公开的方法。

语法

接口指定其他类实现的相关成员集。 它有以下语法

// 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

请注意

1、在接口声明中,成员未实现。

2、成员是抽象的,由default关键字声明。 但是,您可以使用default关键字提供默认实现。
3、您可以通过使用对象表达式或使用类类型实现接口。
4、在类或对象实现中,您需要为接口的抽象方法提供方法体。
5、关键字interfaceend(标记定义的开始和结束)是可选的。
例如,

type IPerson =
   abstract Name : string
   abstract Enter : unit -> unit
   abstract Leave : unit -> unit

调用接口方法

接口方法通过接口调用,而不是通过实例的类或类型实现接口。 要调用接口方法,您可以使用:>运算符(upcast运算符)转换为接口类型。
例如,

(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
由JSRUN为你提供的F#在线运行、在线编译工具
        JSRUN提供的F# 在线运行,F# 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout