我有一个课程服务和一个界面服务。我想将这两个实现为 ExampleService 类。
我知道C#,在C#中我们可以使用下面的代码
来做到这一点Maininterfacecontroller
我怎么能用F#谢谢。
答案 0 :(得分:5)
type ExampleService() =
inherit Service()
interface IService with
// Below is interface implementation
member x.ImplementedInterfaceMember() =
...
答案 1 :(得分:4)
首先,让我们看一下F#wiki页面here:
中的界面语法open System
type Person(name : string, age : int) =
member this.Name = name
member this.Age = age
(* IComparable is used for ordering instances *)
interface IComparable<Person> with
member this.CompareTo(other) =
(* sorts by name, then age *)
match this.Name.CompareTo(other.Name) with
| 0 -> this.Age.CompareTo(other.Age)
| n -> n
(* Used for comparing this type against other types *)
interface IEquatable<string> with
member this.Equals(othername) = this.Name.Equals(othername)
如您所见,接口包含在类中,其功能通过关键字with
来描述。
现在让我们看一下官方MSDN网站中描述的继承模块:
type MyClassBase1() =
let mutable z = 0
abstract member function1 : int -> int
default u.function1(a : int) = z <- z + a; z
type MyClassDerived1() =
inherit MyClassBase1()
override u.function1(a: int) = a + 1
这里,另一个类写在外面,实现/覆盖在关键字inherits
之后。
所以,在你的情况下:
type Service()=
(* Service parent class details here *)
type ExampleService() =
inherit Service()
(* More stuff about the parent class here, i.e overrides *)
interface IService<ExampleClass> with
(* Add here the interface details. *)