# Previously

请先行了解 👉 Interface

# 定义 Typescript implements 关键字

implements

implements (实现) (opens new window) 是面向对象中的一个重要概念。它是要实现一个已经定义好的接口中的方法。

# 使用

# 接口实现

interface Shape {
  color: string;
  draw(): void;
}
class DrawShape implements Shape {
  color: string;
  
	constructor(color: string) {
    this.color = color;
  }
  
  draw() {
    console.log(`draw shape with ${this.color}`);
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

一个类可以实现多个接口

interface Shape {
  color: string;
  draw(): void;
}

interface Paint {
  paint(): void;
}

class DrawShape implements Shape, Paint {
  color: string;
  
	constructor(color: string) {
    this.color = color;
  }
  
  draw() {
    console.log(`draw shape with ${this.color}`);
  }
  
  paint() {
    console.log(`draw shape with ${this.color}`);
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 快来耍耍啊

# 🌰🌰

// template
1

# 游乐场


# 参考答案

// answer
1

# 参考资料

whats-the-difference-between-extends-and-implements-in-typescript (opens new window)