■ 接口
接口只具有抽象方法和属性。同时,接口不提供实现。也就是说,不能直接实例化接口,必须通过后文叙述的Object Expression,用类来实现接口。
接口的书写方法如下所示。
-
接口
-
type 接口名 = interface
声明继承
成员定义
end
interface与end在#light语句中可以省略。
下例中将抽象方法一节中的介绍中创建的抽象方法改变成接口。
接口的例子
type AnimalInterface = interface |
下例中使用接口将上一节抽象方法中的代码进行改写。
使用接口的例子
type Dog = class |
接口的实现如下文所示
-
接口的实现
-
interface 接口名 with
值或成员的定义
end
稍嫌麻烦的是,不能写成象上例中注释掉的部分那样的写法.如果想不转换类型而可以完成上述功能的话,必须要写成下面这样。
使用接口的例子之2
type AnimalInterface = interface |
但是,这样的话同样内容的函数需要写两遍,就失去了接口的意义了。还有,接口内的函数与类内的函数也分开来执行了,
■ 接口的继承
接口可以继承接口。继承接口时使用inherit关键字。
接口的继承
type IA = interface abstract One : int -> int end;; |
一个接口可以继承多个接口。上例中IC继承IA和IB,带有One,Two,Three三个抽象方法。在下例中将这三个方法作为函数进行实现。
继承后的接口的利用
type ClassWithIC = class |
象这样,也可以象利用普通接口那样利用继承后创建的接口。