Skip to content
静心静心
HOME
DoubtfulCases
github icon
  • Typescript基础

    • 原始数据类型
      • 任意值
        • 类型推论
          • 联合类型
            • 对象的类型——接口
              • 数组的类型
                • 函数的类型
                  • 类型断言
                    • 声明文件
                      • 内置对象
                      • Typescript进阶

                        • 类型别名
                          • 字符串字面量类型
                            • 元组
                              • 枚举
                                • 类
                                  • 类的概念
                                    • ES6 中类的用法
                                      • 属性和方法
                                        • 类的继承
                                          • 存取器
                                            • 静态方法
                                            • ES7 中类的用法
                                              • 实例属性
                                                • 静态属性
                                                • TypeScript 中类的用法
                                                  • public private 和 protected
                                                    • 参数属性
                                                      • readonly
                                                        • 抽象类
                                                        • 类的类型
                                                          • 参考
                                                          • 类与接口
                                                            • 泛型
                                                              • 声明合并
                                                                • 扩展阅读

                                                                类

                                                                author iconYYtimer icon大约 7 分钟

                                                                此页内容
                                                                • 类的概念
                                                                • ES6 中类的用法
                                                                  • 属性和方法
                                                                  • 类的继承
                                                                  • 存取器
                                                                  • 静态方法
                                                                • ES7 中类的用法
                                                                  • 实例属性
                                                                  • 静态属性
                                                                • TypeScript 中类的用法
                                                                  • public private 和 protected
                                                                  • 参数属性
                                                                  • readonly
                                                                  • 抽象类
                                                                • 类的类型
                                                                • 参考

                                                                # 类

                                                                传统方法中,JavaScript 通过构造函数实现类的概念,通过原型链实现继承。而在 ES6 中,我们终于迎来了 class。

                                                                TypeScript 除了实现了所有 ES6 中的类的功能以外,还添加了一些新的用法。

                                                                这一节主要介绍类的用法,下一节再介绍如何定义类的类型。

                                                                # 类的概念

                                                                虽然 JavaScript 中有类的概念,但是可能大多数 JavaScript 程序员并不是非常熟悉类,这里对类相关的概念做一个简单的介绍。

                                                                • 类(Class):定义了一件事物的抽象特点,包含它的属性和方法
                                                                • 对象(Object):类的实例,通过 new 生成
                                                                • 面向对象(OOP)的三大特性:封装、继承、多态
                                                                • 封装(Encapsulation):将对数据的操作细节隐藏起来,只暴露对外的接口。外界调用端不需要(也不可能)知道细节,就能通过对外提供的接口来访问该对象,同时也保证了外界无法任意更改对象内部的数据
                                                                • 继承(Inheritance):子类继承父类,子类除了拥有父类的所有特性外,还有一些更具体的特性
                                                                • 多态(Polymorphism):由继承而产生了相关的不同的类,对同一个方法可以有不同的响应。比如 Cat 和 Dog 都继承自 Animal,但是分别实现了自己的 eat 方法。此时针对某一个实例,我们无需了解它是 Cat 还是 Dog,就可以直接调用 eat 方法,程序会自动判断出来应该如何执行 eat
                                                                • 存取器(getter & setter):用以改变属性的读取和赋值行为
                                                                • 修饰符(Modifiers):修饰符是一些关键字,用于限定成员或类型的性质。比如 public 表示公有属性或方法
                                                                • 抽象类(Abstract Class):抽象类是供其他类继承的基类,抽象类不允许被实例化。抽象类中的抽象方法必须在子类中被实现
                                                                • 接口(Interfaces):不同类之间公有的属性或方法,可以抽象成一个接口。接口可以被类实现(implements)。一个类只能继承自另一个类,但是可以实现多个接口

                                                                # ES6 中类的用法

                                                                下面我们先回顾一下 ES6 中类的用法,更详细的介绍可以参考 ECMAScript 6 入门 - Classopen in new window。

                                                                # 属性和方法

                                                                使用 class 定义类,使用 constructor 定义构造函数。

                                                                通过 new 生成新实例的时候,会自动调用构造函数。

                                                                class Animal {
                                                                    name;
                                                                    constructor(name) {
                                                                        this.name = name;
                                                                    }
                                                                    sayHi() {
                                                                        return `My name is ${this.name}`;
                                                                    }
                                                                }
                                                                
                                                                let a = new Animal('Jack');
                                                                console.log(a.sayHi()); // My name is Jack
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12

                                                                # 类的继承

                                                                使用 extends 关键字实现继承,子类中使用 super 关键字来调用父类的构造函数和方法。

                                                                class Cat extends Animal {
                                                                  constructor(name) {
                                                                    super(name); // 调用父类的 constructor(name)
                                                                    console.log(this.name);
                                                                  }
                                                                  sayHi() {
                                                                    return 'Meow, ' + super.sayHi(); // 调用父类的 sayHi()
                                                                  }
                                                                }
                                                                
                                                                let c = new Cat('Tom'); // Tom
                                                                console.log(c.sayHi()); // Meow, My name is Tom
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12

                                                                # 存取器

                                                                使用 getter 和 setter 可以改变属性的赋值和读取行为:

                                                                class Animal {
                                                                  constructor(name) {
                                                                    this.name = name;
                                                                  }
                                                                  get name() {
                                                                    return 'Jack';
                                                                  }
                                                                  set name(value) {
                                                                    console.log('setter: ' + value);
                                                                  }
                                                                }
                                                                
                                                                let a = new Animal('Kitty'); // setter: Kitty
                                                                a.name = 'Tom'; // setter: Tom
                                                                console.log(a.name); // Jack
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12
                                                                13
                                                                14
                                                                15

                                                                # 静态方法

                                                                使用 static 修饰符修饰的方法称为静态方法,它们不需要实例化,而是直接通过类来调用:

                                                                class Animal {
                                                                  static isAnimal(a) {
                                                                    return a instanceof Animal;
                                                                  }
                                                                }
                                                                
                                                                let a = new Animal('Jack');
                                                                Animal.isAnimal(a); // true
                                                                a.isAnimal(a); // TypeError: a.isAnimal is not a function
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9

                                                                # ES7 中类的用法

                                                                ES7 中有一些关于类的提案,TypeScript 也实现了它们,这里做一个简单的介绍。

                                                                # 实例属性

                                                                ES6 中实例的属性只能通过构造函数中的 this.xxx 来定义,ES7 提案中可以直接在类里面定义:

                                                                class Animal {
                                                                  name = 'Jack';
                                                                
                                                                  constructor() {
                                                                    // ...
                                                                  }
                                                                }
                                                                
                                                                let a = new Animal();
                                                                console.log(a.name); // Jack
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10

                                                                # 静态属性

                                                                ES7 提案中,可以使用 static 定义一个静态属性:

                                                                class Animal {
                                                                  static num = 42;
                                                                
                                                                  constructor() {
                                                                    // ...
                                                                  }
                                                                }
                                                                
                                                                console.log(Animal.num); // 42
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9

                                                                # TypeScript 中类的用法

                                                                # public private 和 protected

                                                                TypeScript 可以使用三种访问修饰符(Access Modifiers),分别是 public、private 和 protected。

                                                                • public 修饰的属性或方法是公有的,可以在任何地方被访问到,默认所有的属性和方法都是 public 的
                                                                • private 修饰的属性或方法是私有的,不能在声明它的类的外部访问
                                                                • protected 修饰的属性或方法是受保护的,它和 private 类似,区别是它在子类中也是允许被访问的

                                                                下面举一些例子:

                                                                class Animal {
                                                                  public name;
                                                                  public constructor(name) {
                                                                    this.name = name;
                                                                  }
                                                                }
                                                                
                                                                let a = new Animal('Jack');
                                                                console.log(a.name); // Jack
                                                                a.name = 'Tom';
                                                                console.log(a.name); // Tom
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11

                                                                上面的例子中,name 被设置为了 public,所以直接访问实例的 name 属性是允许的。

                                                                很多时候,我们希望有的属性是无法直接存取的,这时候就可以用 private 了:

                                                                class Animal {
                                                                  private name;
                                                                  public constructor(name) {
                                                                    this.name = name;
                                                                  }
                                                                }
                                                                
                                                                let a = new Animal('Jack');
                                                                console.log(a.name);
                                                                a.name = 'Tom';
                                                                
                                                                // index.ts(9,13): error TS2341: Property 'name' is private and only accessible within class 'Animal'.
                                                                // index.ts(10,1): error TS2341: Property 'name' is private and only accessible within class 'Animal'.
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12
                                                                13

                                                                需要注意的是,TypeScript 编译之后的代码中,并没有限制 private 属性在外部的可访问性。

                                                                上面的例子编译后的代码是:

                                                                var Animal = (function () {
                                                                  function Animal(name) {
                                                                    this.name = name;
                                                                  }
                                                                  return Animal;
                                                                })();
                                                                var a = new Animal('Jack');
                                                                console.log(a.name);
                                                                a.name = 'Tom';
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9

                                                                使用 private 修饰的属性或方法,在子类中也是不允许访问的:

                                                                class Animal {
                                                                  private name;
                                                                  public constructor(name) {
                                                                    this.name = name;
                                                                  }
                                                                }
                                                                
                                                                class Cat extends Animal {
                                                                  constructor(name) {
                                                                    super(name);
                                                                    console.log(this.name);
                                                                  }
                                                                }
                                                                
                                                                // index.ts(11,17): error TS2341: Property 'name' is private and only accessible within class 'Animal'.
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12
                                                                13
                                                                14
                                                                15

                                                                而如果是用 protected 修饰,则允许在子类中访问:

                                                                class Animal {
                                                                  protected name;
                                                                  public constructor(name) {
                                                                    this.name = name;
                                                                  }
                                                                }
                                                                
                                                                class Cat extends Animal {
                                                                  constructor(name) {
                                                                    super(name);
                                                                    console.log(this.name);
                                                                  }
                                                                }
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12
                                                                13

                                                                当构造函数修饰为 private 时,该类不允许被继承或者实例化:

                                                                class Animal {
                                                                  public name;
                                                                  private constructor(name) {
                                                                    this.name = name;
                                                                  }
                                                                }
                                                                class Cat extends Animal {
                                                                  constructor(name) {
                                                                    super(name);
                                                                  }
                                                                }
                                                                
                                                                let a = new Animal('Jack');
                                                                
                                                                // index.ts(7,19): TS2675: Cannot extend a class 'Animal'. Class constructor is marked as private.
                                                                // index.ts(13,9): TS2673: Constructor of class 'Animal' is private and only accessible within the class declaration.
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12
                                                                13
                                                                14
                                                                15
                                                                16

                                                                当构造函数修饰为 protected 时,该类只允许被继承:

                                                                class Animal {
                                                                  public name;
                                                                  protected constructor(name) {
                                                                    this.name = name;
                                                                  }
                                                                }
                                                                class Cat extends Animal {
                                                                  constructor(name) {
                                                                    super(name);
                                                                  }
                                                                }
                                                                
                                                                let a = new Animal('Jack');
                                                                
                                                                // index.ts(13,9): TS2674: Constructor of class 'Animal' is protected and only accessible within the class declaration.
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12
                                                                13
                                                                14
                                                                15

                                                                # 参数属性

                                                                修饰符和readonly还可以使用在构造函数参数中,等同于类中定义该属性同时给该属性赋值,使代码更简洁。

                                                                class Animal {
                                                                  // public name: string;
                                                                  public constructor(public name) {
                                                                    // this.name = name;
                                                                  }
                                                                }
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6

                                                                # readonly

                                                                只读属性关键字,只允许出现在属性声明或索引签名或构造函数中。

                                                                class Animal {
                                                                  readonly name;
                                                                  public constructor(name) {
                                                                    this.name = name;
                                                                  }
                                                                }
                                                                
                                                                let a = new Animal('Jack');
                                                                console.log(a.name); // Jack
                                                                a.name = 'Tom';
                                                                
                                                                // index.ts(10,3): TS2540: Cannot assign to 'name' because it is a read-only property.
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12

                                                                注意如果 readonly 和其他访问修饰符同时存在的话,需要写在其后面。

                                                                class Animal {
                                                                  // public readonly name;
                                                                  public constructor(public readonly name) {
                                                                    // this.name = name;
                                                                  }
                                                                }
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6

                                                                # 抽象类

                                                                abstract 用于定义抽象类和其中的抽象方法。

                                                                什么是抽象类?

                                                                首先,抽象类是不允许被实例化的:

                                                                abstract class Animal {
                                                                  public name;
                                                                  public constructor(name) {
                                                                    this.name = name;
                                                                  }
                                                                  public abstract sayHi();
                                                                }
                                                                
                                                                let a = new Animal('Jack');
                                                                
                                                                // index.ts(9,11): error TS2511: Cannot create an instance of the abstract class 'Animal'.
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11

                                                                上面的例子中,我们定义了一个抽象类 Animal,并且定义了一个抽象方法 sayHi。在实例化抽象类的时候报错了。

                                                                其次,抽象类中的抽象方法必须被子类实现:

                                                                abstract class Animal {
                                                                  public name;
                                                                  public constructor(name) {
                                                                    this.name = name;
                                                                  }
                                                                  public abstract sayHi();
                                                                }
                                                                
                                                                class Cat extends Animal {
                                                                  public eat() {
                                                                    console.log(`${this.name} is eating.`);
                                                                  }
                                                                }
                                                                
                                                                let cat = new Cat('Tom');
                                                                
                                                                // index.ts(9,7): error TS2515: Non-abstract class 'Cat' does not implement inherited abstract member 'sayHi' from class 'Animal'.
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12
                                                                13
                                                                14
                                                                15
                                                                16
                                                                17

                                                                上面的例子中,我们定义了一个类 Cat 继承了抽象类 Animal,但是没有实现抽象方法 sayHi,所以编译报错了。

                                                                下面是一个正确使用抽象类的例子:

                                                                abstract class Animal {
                                                                  public name;
                                                                  public constructor(name) {
                                                                    this.name = name;
                                                                  }
                                                                  public abstract sayHi();
                                                                }
                                                                
                                                                class Cat extends Animal {
                                                                  public sayHi() {
                                                                    console.log(`Meow, My name is ${this.name}`);
                                                                  }
                                                                }
                                                                
                                                                let cat = new Cat('Tom');
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12
                                                                13
                                                                14
                                                                15

                                                                上面的例子中,我们实现了抽象方法 sayHi,编译通过了。

                                                                需要注意的是,即使是抽象方法,TypeScript 的编译结果中,仍然会存在这个类,上面的代码的编译结果是:

                                                                var __extends =
                                                                  (this && this.__extends) ||
                                                                  function (d, b) {
                                                                    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
                                                                    function __() {
                                                                      this.constructor = d;
                                                                    }
                                                                    d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
                                                                  };
                                                                var Animal = (function () {
                                                                  function Animal(name) {
                                                                    this.name = name;
                                                                  }
                                                                  return Animal;
                                                                })();
                                                                var Cat = (function (_super) {
                                                                  __extends(Cat, _super);
                                                                  function Cat() {
                                                                    _super.apply(this, arguments);
                                                                  }
                                                                  Cat.prototype.sayHi = function () {
                                                                    console.log('Meow, My name is ' + this.name);
                                                                  };
                                                                  return Cat;
                                                                })(Animal);
                                                                var cat = new Cat('Tom');
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12
                                                                13
                                                                14
                                                                15
                                                                16
                                                                17
                                                                18
                                                                19
                                                                20
                                                                21
                                                                22
                                                                23
                                                                24
                                                                25
                                                                26

                                                                # 类的类型

                                                                给类加上 TypeScript 的类型很简单,与接口类似:

                                                                class Animal {
                                                                  name: string;
                                                                  constructor(name: string) {
                                                                    this.name = name;
                                                                  }
                                                                  sayHi(): string {
                                                                    return `My name is ${this.name}`;
                                                                  }
                                                                }
                                                                
                                                                let a: Animal = new Animal('Jack');
                                                                console.log(a.sayHi()); // My name is Jack
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12

                                                                # 参考

                                                                • Classesopen in new window(中文版open in new window)
                                                                • ECMAScript 6 入门 - Classopen in new window
                                                                edit icon编辑此页open in new window
                                                                上一页
                                                                枚举
                                                                下一页
                                                                类与接口
                                                                傻瓜都能写出计算机可以理解的代码。唯有能写出人类容易理解的代码的,才是优秀的程序员。
                                                                Copyright © 2022 YY

                                                                该应用可以安装在您的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和您的操作系统安全地进行交互。

                                                                详情