Allen Yu Allen Yu
Home
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《TypeScript入门教程》
GitHub (opens new window)
Home
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《TypeScript入门教程》
GitHub (opens new window)
  • 开始

  • 基础

  • 进阶

    • 类型别名
    • 字符串字面量类型
    • 元组
    • 枚举
    • 类
    • 类与接口
      • 类与接口 ( Class And Interface)
      • 类实现接口
      • 接口继承接口
      • 接口继承类
      • 参考
    • 泛型
    • 声明合并
    • 工具类型
    • 编译配置
  • 实战

  • TypeScript
  • 进阶
2020-10-31
目录

类与接口

# 类与接口 ( Class And Interface)

之前学习过,接口可以用于对「对象的形状」进行描述。

# 类实现接口

实现(implements) 是面向对象中的重要概念。一般来讲,一个类只能继承至另一个类,有时候不同类之间可以有一些共有的特性,这时候就可以把特性提取成接口,用 implements 关键字来实现。这个特性大大提高了面向对象的灵活性。

举例来说,门是一个类,防盗门是门的子类。如果防盗门有一个报警器的功能,我们可以简单的给防盗门添加一个报警方法。这时候如果有另一个类,车,也有报警器的功能,就可以考虑把报警器提取出来,作为一个接口,防盗门和车都去实现它。

interface Alarm {
  alert(): void
}
class Door {}
class SecurityDoor extends Door implements Alarm {
  alert() {
    console.log('SecurityDoor alert!')
  }
}
class Car implements Alarm {
  alert() {
    console.log('Car alert!')
  }
}

一个类可以实现多个接口

interface Alarm {
  alert(): void
}

interface Light {
  lightOn(): void
  lightOff(): void
}

class Car implements Alarm, Light {
  alert() {
    console.log('Car alert!')
  }

  lightOn() {
    console.log('Car light on')
  }

  lightOff() {
    console.log('Car light off')
  }
}

上例中, Car 实现了 Alarm 和 Light 接口,既能报警,也能开关车灯。

# 接口继承接口

接口与接口之间可以是继承关系:

interface Alarm {
  alert(): void
}

interface LightableAlarm extends Alarm {
  lightOn(): void
  lightOff(): void
}

这很好理解,LightableAlarm 继承至 Alarm,除了拥有 alert 方法外,还拥有两个新方法 lightOn 和 lightOff。

# 接口继承类

常见的面向对象语言中,接口是不能继承类的,但是在 TypeScript 中却是可以的:

class Point {
  x: number
  y: number
  constructor(x: number, y: number) {
    this.x = x
    this.y = y
  }
}

interface Point3d extends Point {
  z: number
}

let point3d: Point3d = { x: 1, y: 2, z: 3 }

为什么 TypeScript 会支持接口继承类呢?

实际上,当我们在声明 class point 时,除了会创建一个名为 Point 的类外,同时也创建了一个名为 Point 的类型。

所以,我们既可以将 Point 当做一个类来使用。

也可以将 Point 当做一个类型来用:

function printPoint(p: Point) {
  console.log(p.x, p.y)
}

printPoint(new Point(1, 2))

这个例子实际上等价于:

class Point {
  x: number
  y: number
  constructor(x: number, y: number) {
    this.x = x
    this.y = y
  }
}

interface PointInstanceType {
  x: number
  y: number
}

function printPoint(p: PointInstanceType) {
  console.log(p.x, p.y)
}

printPoint(new Point(1, 2))

上例中,我们新声明的 PointInstanceType 和声明 class Point 时,创建的 Point 类型是等价的。

所以,回到 Point3d 的例子中,我们就能很容易的理解,为什么 TypeScript 会支持接口继承类了:

class Point {
  x: number
  y: number
  constructor(x: number, y: number) {
    this.x = x
    this.y = y
  }
}

interface PointIntanceType {
  x: number
  y: number
}

interface Point3d extends Point {
  z: number
}

let point3d: Point3d = { x: 1, y: 2, z: 3 }

当我们声明 interface Point3d extends Point 时,Point3d 继承的实际上是类 Point 的实例的类型。

换句话说,可以理解为定义了一个接口 Point3d 继承另一个接口 PointInstanceType。

所以,「接口继承类」和 「接口继承接口」没有什么本质的区别。

值得注意的是,PointInstanceType 相比于 Point,缺少了 constructor 方法,这是因为声明 Point 类时创建的 Point 类型是不包含构造函数的。另外,除了构造函数是不包含的,静态属性或静态方法也是不包含的(实例的类型,当然不应该包含构造函数、静态属性或静态方法)。

换句话说,声明 Point 类时创建的 Point 类型只包含其中的实例属性和实例方法。

class Point {
  static origin = new Point(0, 0)
  static distanceToOrigin(p: Point) {
    return Math.sqrt(p.x * p.x + p.y * p.y)
  }
  x: number
  y: number
  constructor(x: number, y: number) {
    this.x = x
    this.y = y
  }
  printPoint() {
    console.log(this.x, this.y)
  }
}

interface PointInstanceType {
  x: number
  y: number
  printPoint(): void
}

let p1: Point
let p2: PointInstanceType

上例中,类型 Point 和 PointInstanceType 是等价的。

同样的,在接口继承类的时候,也只会继承它的实例属性和实例方法。

# 参考

  • TypeScript 入门教程 - 类与接口 (opens new window)
Last Updated: 3/30/2022, 1:20:25 AM
类
泛型

← 类 泛型→

最近更新
01
4 月第 2 周总结
04-17
02
工具类型
04-01
03
Google 搜索小技巧
03-28
更多文章>
Theme by Vdoing | Copyright © 2018-2022 Allen Yu
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式