Extending vs. implementing a pure abstract class in TypeScript

前端 未结 4 1574
清酒与你
清酒与你 2020-12-02 12:01

Suppose I have a pure abstract class (that is, an abstract class without any implementation):

abstract class A {
    abstract m(): void;
}

4条回答
  •  渐次进展
    2020-12-02 12:29

    Building on @toskv's answer, if you extend an abstract class, you have to call super() in the subclass's constructor. If you implement the abstract class, you don't have to call super() (but you have to implement all the methods declared in the abstract class, including private methods).

    Implementing an abstract class instead of extending it could be useful if you want to create a mock class for testing without having to worry about the original class's dependencies and constructor.

提交回复
热议问题