typescript abstract class abstract

The abstract class also becomes the base class, similar to defining a template, the subclasses of the abstract class must define the abstract methods and abstract properties in the abstract class in the same way as the abstract class.

After reading the above paragraph, it feels like. . . It doesn't feel much.

What is an abstract class?
Just add an abstract modifier in front of the class.

// Person加了abstract 就是一个抽象类
abstract class Person {
    
    
  name: string;
  age: number;
  sex: '男' | '女';
  constructor(name: string, age: number, sex: '男' | '女') {
    
    
    this.name = name;
    this.age = age;
    this.sex = sex;
  }
}

What are abstract methods and abstract properties?
In an abstract class, add an abstract modifier to a certain method or attribute

abstract class Person {
    
    
  abstract name: string;
  age: number;
  sex: '男' | '女';
  constructor(name: string, age: number, sex: '男' | '女') {
    
    
    this.name = name;
    this.age = age;
    this.sex = sex;
  }
  getName: () => void = () => {
    
    
    return this.name;
  };
// getPerosonInfo方法加了abstract 就是一个抽象方法
  abstract getPerosonInfo: () => void = () => {
    
    
    return {
    
    
      name: this.name,
    };
  };
}

There can be no abstract methods or abstract properties in an abstract class, but the abstract methods or abstract properties must be placed in the abstract class.

abstract class Tz {
    
    
  getPerosonInfo: () => void = () => {
    
    
  }
}
class Yx {
    
    
  constructor() {
    
    

  }
  /** 抽象方法只能放到抽象类中, */ 
  // abstract getName = () => {
    
    
    // 
  // }
}

The abstract class cannot create an instance (the new operation cannot be performed)

/** class 抽象 abstract */

abstract class Person {
    
    
  name: string;
  age: number;
  sex: '男' | '女';
  constructor(name: string, age: number, sex: '男' | '女') {
    
    
    this.name = name;
    this.age = age;
    this.sex = sex;
  }
  getName: () => void = () => {
    
    
    return this.name;
  };

  abstract getPerosonInfo: () => void = () => {
    
    
    return {
    
    
      name: this.name,
    };
  };
}
// 抽象类不能实例化
// var person1 = new Person('dx', 18, '男');

The abstract class is the base class, and the subclass must have abstract methods or abstract properties of the abstract class (and the types are consistent)

/** class abstract abstract */

// 抽象父类(基类)
abstract class Person {
    
    
  name: string;
  age: number;
  sex: '男' | '女';
  constructor(name: string, age: number, sex: '男' | '女') {
    
    
    this.name = name;
    this.age = age;
    this.sex = sex;
  }
  getName: () => void = () => {
    
    
    return this.name;
  };

  abstract getPerosonInfo: () => void = () => {
    
    
    return {
    
    
      name: this.name,
    };
  };
}

// 子类 继承抽象类
class Dx extends Person {
    
    
//父类的抽象方法必须存在子类中,并且类型一致
  getPerosonInfo: () => void = () => {
    
    
    return {
    
    
      name: this.name,
    };
  };
}

// class Tz extends Person {
    
    
  // 类型不一致是不允许的
  // getPerosonInfo: (name: string) => void = () => {};
// }

The role of an abstract class, as a template of the class, standardizes the definition of its subclasses and makes the code more standardized.

Guess you like

Origin blog.csdn.net/glorydx/article/details/111321401