实现接口的类
实现( implements )是面向对象中的一个重要概念。一般来讲,一个类只能继承自另一个类,有时候不同类之间可以有一些共有的特性,这时候就可以把特性提取成接口( interfaces ),用 implements 关键字来实现。这个特性大大提高了面向对象的灵活性。
举例来说,门是一个类,防盗门是门的子类。如果防盗门有一个报警器的功能,我们可以简单的给防盗门添加一个报警方法。这时候如果有另一个类,车,也有报警器的功能,就可以考虑把报警器提取出来,作为一个接口,防盗门和车都去实现它。
interface ClockInterface {currentTime: Date;}class Clock implements ClockInterface {public currentTime: Date = new Date();constructor(h: number, m: number) { }}
也可以在接口中描述一个方法,在类里实现它,如同下面的setTime方法一样:
interface ClockInterface {currentTime: Date;setTime(d: Date): void;}class Clock implements ClockInterface {public currentTime: Date = new Date();constructor(h: number, m: number) { }public setTime(d: Date) {this.currentTime = d;}}
接口描述了类的公共部分,而不是公共和私有两部分。 它不会帮你检查类是否具有某些私有成员。
一个类可以实现多个接口
interface ClockInterface {currentTime: Date;setTime(d: Date): void;}interface LogTime {log(): void;}class Clock implements ClockInterface, LogTime {public currentTime: Date = new Date();constructor(public h: number, public m: number) { }public setTime(d: Date) {this.currentTime = d;}public log() {console.log(this.currentTime);}}
接口的继承
和类一样,接口也可以相互继承。 这让我们能够从一个接口里复制成员到另一个接口里,可以更灵活地将接口分割到可重用的模块里。
interface Shape {color: string;}interface Square extends Shape {sideLength: number;}let square = {} as Square;square.color = "blue";square.sideLength = 10;
一个接口可以继承多个接口
interface Shape {color: string;}interface PenStroke {penWidth: number;}interface Square extends Shape, PenStroke {sideLength: number;}let square = {} as Square;square.color = "blue";square.sideLength = 10;square.penWidth = 5.0;
接口继承类
当接口继承了一个类类型时,它会继承类的成员但不包括其实现。 就好像接口声明了所有类中存在的成员,但并没有提供具体实现一样。 接口同样会继承到类的 private 和 protected 成员。 这意味着当你创建了一个接口继承了一个拥有私有或受保护的成员的类时,这个接口类型只能被这个类或其子类所实现( implement)。
当你有一个庞大的继承结构时这很有用,但要指出的是你的代码只在子类拥有特定属性时起作用。 除了继承自基类,子类之间不必相关联。
class Control {protected state: any;}interface SelectableControl extends Control {select(): void;}class Button extends Control implements SelectableControl {public select() {console.log("select");}}class TextBox extends Control {public select() {console.log("select");}}class Images extends Control implements SelectableControl {constructor() {super();}public select() {console.log(this.state);}}
在上面的例子里,
SelectableControl包含了Control的所有成员,包括私有成员state。 因为state是私有成员但可被子类访问,所以只能够是Control的子类们才能实现SelectableControl接口。 因为只有Control的子类才能够拥有一个声明于Control的私有成员state,这对私有成员的兼容性是必需的。 在Control类内部,是允许通过SelectableControl的实例来访问私有成员state的。 实际上,SelectableControl就像Control一样,并拥有一个select方法。Button和TextBox类是SelectableControl的子类(因为它们都继承自Control并有select方法)
这里其实我将官网的例子加以改造了,按照官网的例子是这样的:
class Control {private state: any;}interface SelectableControl extends Control {select(): void;}class Button extends Control implements SelectableControl {select() { }}class TextBox extends Control {select() { }}// Error: Property 'state' is missing in type 'Image'.class Image implements SelectableControl {select() { }}class Location {}
在这个例子里,state作为一个私有属性,是不可以被声明类以外的类访问的。
如果以上的例子都过于复杂的话,这里提供一个相对简单的例子,这个例子也可以看作是一个将类当作接口使用的例子:
class Point {public x: number;public y: number;}interface Point3D extends Point {z: number;}let point3d: Point3D = {x: 1, y: 2, z: 3};
混合使用
我们在之前的学习中知道了,我们可以使用接口的方式来定义一个函数需要符合的形状:
type SearchFunc = (source: string, subString: string) => boolean;let mySearch: SearchFunc;mySearch = (source: string, subString: string) => {return source.search(subString) !== -1;};console.log(mySearch("zxc", "z"));
有时候,一个函数还可以有自己的属性和方法:
interface Counter {(start: number): string;interval: number;reset(): void;}function getCounter(): Counter {const fun: (start: number) => string = (starts: number): string => {return starts.toString();};const fun2: () => void = (): void => {console.log("void func");};const counter = fun as Counter;counter.interval = 123;counter.reset = fun2;return counter;}const c = getCounter();c(10);c.reset();c.interval = 5.0;
