前言

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

ES6中类的语法

class的基本语法

ES7中类的用法

说明:在es7中有一些关于类的提案

实例属性:在es6中实例的属性只能通过this.xxx来定义,在es7中可以这样去定义

  1. class Father {
  2. name = 'along';
  3. constructor () {
  4. }
  5. }
  6. let p = new Father();
  7. p.name // along

静态属性:在es7中可以使用static定义一个静态属性

  1. class Father {
  2. static age = 18;
  3. constructor () {
  4. }
  5. }
  6. Father.age // 18

TypeScript中类的用法

TypeScript可以使用三种访问修饰符,分别是public,private,protected.

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

1.public的案例,name属性被设置为公有的,是可以被访问修改的

  1. class Father {
  2. public age;
  3. public constructor (age) {
  4. this.age = 18;
  5. }
  6. }
  7. let p = new Father('18');
  8. p.age // 18
  9. p.age = 19;
  10. p.age // 19

2.如果把name的定义改为private会发生什么呢?

  1. class Father {
  2. private age;
  3. public constructor (age) {
  4. this.age = 18;
  5. }
  6. }
  7. let p = new Father('18');
  8. p.age //这时候编译器就会给我们报一个提示,属性“age”为私有属性,只能在类“Father”中访问,编译后还是可以正常执行的

3.那么在子类可以访问age变量嘛?可以看到,在编译器中会给我们提示.

  1. class Father {
  2. private age;
  3. public constructor (age) {
  4. this.age = 18;
  5. }
  6. }
  7. let p = new Father('18');
  8. class Son extends Father {
  9. constructor (age) {
  10. super(age);
  11. console.log('age:' + this.age);//属性“age”为私有属性,只能在类“Father”中访问。
  12. }
  13. }
  14. new Son('18');

4.当构造函数修饰为private时,该类不允许被被实例化

  1. class Father {
  2. protected age;
  3. private constructor (age) {
  4. this.age = 18;
  5. }
  6. }
  7. new Father('18'); //类“Father”的构造函数是私有的,仅可在类声明中访问。

5.当构造函数修饰为private时,该类不允许被被继承

  1. class Father {
  2. protected age;
  3. private constructor (age) {
  4. this.age = 18;
  5. }
  6. }
  7. class Son extends Father {//无法扩展类“Father”。类构造函数标记为私有。
  8. constructor (age) {
  9. super(age);
  10. console.log('age:' + this.age);
  11. }
  12. }

6.当变量使用protected修饰时,允许在子类中访问

  1. class Father {
  2. protected age;
  3. public constructor (age) {
  4. this.age = 18;
  5. }
  6. }
  7. let p = new Father('18');
  8. class Son extends Father {
  9. constructor (age) {
  10. super(age);
  11. console.log('age:' + this.age); //age:18
  12. }
  13. }
  14. new Son('18');

7.当构造函数使用protected修饰时,该类不允许被实例化

  1. class Father {
  2. protected age;
  3. protected constructor (age) {
  4. this.age = 18;
  5. }
  6. }
  7. new Father('18');//类“Father”的构造函数是受保护的,仅可在类声明中访问。

8.当构造函数使用protected修饰时,该类允许被继承

  1. class Father {
  2. protected age;
  3. protected constructor (age) {
  4. this.age = 18;
  5. }
  6. }
  7. class Son extends Father {
  8. constructor (age) {
  9. super(age);
  10. console.log('age:' + this.age);//age:18
  11. }
  12. }
  13. new Son('18');

readonly

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

  1. class Father {
  2. readonly age;
  3. public constructor (age) {
  4. this.age = 18;
  5. }
  6. }
  7. let p = new Father('18');
  8. p.age //18
  9. p.age = 19;// Cannot assign to 'age' because it is a read-only property.

注意:如果和其它修饰符在一块,需要写到后面,实际生效的还是readonly

  1. class Father {
  2. public readonly age;
  3. public constructor (age) {
  4. this.age = 18;
  5. }
  6. }

参数属性

修饰符和readonly可以使用到构造函数中,使用到过程中,readonly要放在最后面.

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

抽象类

  1. abstract用于定义抽象类
  2. 抽象类不允许被实例化
  3. 抽象类的抽象方法必须有子类实现 ```javascript abstract class Father { public age; public constructor (age) { this.age = age; } abstract say ();//定义抽象类方法 }

class Son extends Father { constructor (age) { super(age); } say () {//抽象类方法必须由子类实现 console.log(‘say:’ + this.age) } }

let p = new Son(‘18’);

p.say()

  1. <a name="AxKqT"></a>
  2. #### 类的类型
  3. 给类加上类型和接口类似,如下
  4. ```javascript
  5. class Father {
  6. age: string;
  7. constructor (age: string) {
  8. this.age = age;
  9. }
  10. say (): string {
  11. return 'hello'
  12. }
  13. }
  14. let p = new Father('18');
  15. p.age //18