The difference between TypeScript extends and implements

(It is lucky to be understood, but not to be understood is not necessarily unfortunate. A person who puts his own value entirely on the understanding of others is often worthless. -Zhou Guoping)

insert image description here

extends

related articles

implements

implements is a keyword used by a class to implement an interface. To implement an interface, all methods in the interface must be implemented.

For example, the following code defines the abstract class AbstractAnimal, and then Dog and Cat respectively implement AbstractAnimal, Mouse inherits AbstractAnimal, Pig inherits AbstractAnimal and implements Dog and Cat.

abstract class AbstractAnimal {
    
    
  nickname = '动物';
  getData () {
    
    
    console.log('AbstractAnimal');
  };

  static staticNickname = '静态动物';
  static staticGetData () {
    
    
    console.log('staticGetData');
  }
}


class Dog implements AbstractAnimal {
    
    
  nickname: string;
  getData () {
    
    
    console.log('dog');
  }
}

class Cat implements AbstractAnimal {
    
    
  nickname: string;
  getData () {
    
    
    console.log('cat');
  }
  getSelfData () {
    
    
    console.log('get cat data');
  }
}

class Mouse extends AbstractAnimal {
    
     }

class Pig extends AbstractAnimal implements Dog, Cat {
    
    
  getSelfData (): void {
    
    
    throw new Error("Method not implemented.");
  }
}

We can also define the interface interface for multiple implementations

interface IAnimal {
    
    
  nickname: string;
  getData ();
}


class Dog implements IAnimal {
    
    
  nickname: string;
  getData () {
    
    
    console.log('dog');
  }
}

class Cat implements IAnimal {
    
    
  nickname: string;
  getData () {
    
    
    console.log('cat');
  }
}


The difference between extends and implements

  1. extends is the inheritance class, and implements is the implementation interface
  2. A class can only inherit one, and an interface can implement multiple
  3. When extends inherits the parent class, it can rewrite the method of the parent class, or call the non-private method of the parent class; implements implements the interface, and must implement all the methods of the interface.
  4. Extends is followed by a class, indicating the inheritance relationship
  5. The implements are followed by the interface, which means implementing the interface (can be multiple)
  6. Inheritance can be understood as inheriting the method of the parent class, and the method in the parent class can be used in the subclass
  7. To implement an interface is to define a method in the interface. This method needs to be implemented by yourself. The interface can be regarded as a standard to unify the business interface specification

The difference between interface and implements

  1. Abstract classes can provide implementation details of member methods, while only public abstract (implicitly declared) methods can exist in interfaces
  2. The member variables and methods in the abstract class can write implementation code, while the members in the interface can only declare variables and methods
  3. Interfaces cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods
  4. A class can only inherit one abstract class, but a class can implement multiple interfaces

Guess you like

Origin blog.csdn.net/qq_42427109/article/details/130605846