This is the tenth day of my participation in wen Challenge

1. When a class implements an interface, it must implement all attributes of the interface, and can define other attributes in the class

interface Human {
  name: string;
  eat(): void;
}

class Asian implements Human {
  constructor(name: string) {
    this.name = name;
  }
  name: string;
  eat() {}
  sleep(){}}Copy the code

2. An interface can restrict only public members of a class

interface Human {
  name: string;
  eat(): void;
}

/* Error: class 'Asian' implements interface 'Human'. The attribute "name" is private in type "Asian", but not in type "Human". * /
class Asian implements Human {
  constructor(name: string) {
    this.name = name;
  }
  private name: string;
  eat() {}
  sleep(){}}Copy the code

3. Interfaces cannot constrain class constructors

interface Human {
  new (name: string) :void;
  name: string;
  eat(): void;
}

/* Error: class 'Asian' implements interface 'Human'. The content provided by type "Asian" does not match the signature "new (name: string): void". ts(2420) */
class Asian implements Human {
  constructor(name: string) {
    this.name = name;
  }
  name: string;
  eat() {}
  sleep(){}}Copy the code

4. Interface inheritance

4.1 Interface Inheritance Interface

Interfaces can inherit from each other like classes, and an interface can inherit from multiple interfaces, by separating reusable interfaces, or by combining multiple interfaces into a single interface

interface Human {
  name: string;
  eat(): void;
}

interface Man extends Human {
  run(): void;
}

interface Child {
  cry(): void;
}

interface Boy extends Man, Child {}

let boy: Boy = {
  name: "".eat() {},
  run() {},
  cry(){}};Copy the code

4.2 Interface Inheritance Classes

Equivalent to the interface to the class members are abstracted out, that is, only the class member interface, and no concrete implementation

class Auto {
  state = 1;
  // private state2 = 2;
}

The state attribute is implied in the interface
interface AutoInterFace extends Auto {}
// Implement the AutoInterFace interface
class C implements AutoInterFace {
  state = 1;
}

// Subclasses of Auto can also implement the AutoInterFace interface
// Since Bus is a subclass of Auto, it naturally inherits the state property, so there is no need to implement it repeatedly
class Bus extends Auto implements AutoInterFace {}
Copy the code

When an interface removes a class member, it removes not only the public member, but also the private member and protected member

conclusion

  • Interfaces can inherit from each other, which enables interface reuse
  • Classes can also inherit from each other, enabling reuse of methods and attributes
  • Interfaces can be implemented through classes, but interfaces can constrain only the common members of a class
  • An interface can extract members of a class, including (common member, private member, protected member).

The last

That is the main content of this article, it is very simple, you are welcome to leave your opinion in the comment section!

Feel the harvest of the students welcome to like, pay attention to a wave!

The articles

  • Front end developers should know Centos/Docker/Nginx/Node/Jenkins (🍡 long)
  • What is the principle of qr code scanning login
  • The most complete ECMAScript walkthrough
  • Package. json that front-end developers need to know