Abstract classes, which are abstract and defined using the abstract keyword, have the following characteristics

  • An object that cannot be created is a class designed to be inherited
  • Abstract methods can be added and can only be defined in abstract classes
  • Subclasses inherit abstract classes and must override abstract methods
// Person abstract class
abstract class Person {
    / / property
    name: string;
    age: number;

    // constructor
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }

    // Abstract methods, no concrete implementation
    abstract call():void;
}

The User class inherits the Person class and implements, and must implement, abstract methods
class User extends Person {
    call() {
        console.log(I was `The ${this.name}This year, IThe ${this.age}At the age of `); }}let p = new Person(); // Error, the abstract class cannot create instance
let user = new User("Xiao Ming".18);
user.call();
Copy the code