简体   繁体   English

接口和抽象类(JS)的区别

[英]The difference between Interface and Abstract class (JS)

The limitation of an abstract class lies in the fact that a sub-class can only extend a single abstract class.抽象类的局限性在于子类只能扩展一个抽象类。 Hence, multiple inheritance is found in Interfaces.因此,在接口中可以找到多重继承。

Interfaces can have conrete methods just like abstract classes, but cannot have instance fields, only public, static, final fields.接口可以像抽象类一样有具体的方法,但不能有实例字段,只有公共、静态、最终字段。

Am I correct when i conclude that the only difference lies in the fact that interfaces simply cannot have instance fields?当我断定唯一的区别在于接口根本不能具有实例字段这一事实时,我是否正确?

interface is often some kind of type declaration, whereas class or abstract class are class declaration, which in JS are just constructors, though they often define a specific "type" of values. interface通常是某种类型声明,而classabstract classclass声明,在 JS 中只是构造函数,尽管它们通常定义特定的值“类型”。 abstract are a special case in between the two because they define a new concrete value (a constructor in JS) but cannot be instantiated without being subclassed. abstract是两者之间的一种特殊情况,因为它们定义了一个新的具体值(JS 中的构造函数),但如果不被子类化就无法实例化。

Bottom line, interfaces are declaration in the space of types, whereas [abstract] class are declaration in the space of values.归根结底, interfaces是类型空间中的声明,而[abstract] class是值空间中的声明。 In typescript for instance you can bridge the two using class implements .例如,在 typescript 中,您可以使用class implements来桥接两者。 In JavaScript the term interface more often refers to the general shape of behaviour of a specific type of value returned by some API (see https://developer.mozilla.org/en-US/docs/Web/API/Event where the term interface is used to describe different kind of events).在 JavaScript 中,术语接口通常指的是某些 API 返回的特定类型值的一般行为形式(请参阅https://developer.mozilla.org/en-US/docs/Web/API/Event ,其中术语interface用于描述不同类型的事件)。

Interfaces only describe what properties and methods should be implemented, and don't describe how methods should work.接口只描述了应该实现哪些属性和方法,并没有描述方法应该如何工作。

But abstract classes may describe how a method works, like in regular classes.但是抽象类可以描述一个方法是如何工作的,就像在普通类中一样。 For example:例如:

abstract class MyClass {
   abstract method_1() // a method with no implementation

   method_2() { // a method with implementation
      // do something
   }
}

Interfaces look like:接口看起来像:

interface MyInterface {
   method_1(): void;
   method_2(): string;
}

I assume your question pertains to Typescript because "interface" and "abstract" declarations can only be used in Typescript.我假设您的问题与 Typescript 有关,因为“接口”和“抽象”声明只能在 Typescript 中使用。

Your conclusion is correct, however there are more than just one difference between them:您的结论是正确的,但是它们之间的区别不止一个:

  • An interface can have only abstract methods, an abstract class can have both abstract and non-abstract methods接口只能有抽象方法,抽象类可以同时有抽象和非抽象方法
  • An interface can only have public members, while an abstract class can have protected and public members.接口只能有公共成员,而抽象类可以有受保护和公共成员。
  • Abstract class can provide the implementation of the interface.抽象类可以提供接口的实现。 Interface can not provide the implementation of an abstract class接口不能提供抽象类的实现

If you would like to dive deeper in the topic in terms of design (still related to interface and abstract class) I would recommend the "composition over inheritance" principle.如果您想在设计方面更深入地研究该主题(仍然与接口和抽象类相关),我会推荐“组合优于继承”原则。

Here: Prefer composition over inheritance?这里: 更喜欢组合而不是继承?

Here: https://en.wikipedia.org/wiki/Composition_over_inheritance这里: https ://en.wikipedia.org/wiki/Composition_over_inheritance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM