In TypeScript, what is the difference between the abstract class of Extensions and Implements

We know that a class can be implemented or extended in TypeScript. Some C # or java based students may be confused about this, because in the above two object-oriented languages, only interfaces can be implemented, but only classes In order to be extendeds. Let's explain how these two keywords differ in TypeScript. I recently found this problem in StackOverflow, so I translated it and recorded it. The original address is: https://stackoverflow.com/questions/35990538/extending-vs-implementing-a-pure-abstract-class- in-typescript / 35990799 # 35990799

Problem Description

Extending vs. implementing a pure abstract class in TypeScript

Suppose I have a clean abstract class A:

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

In terms of inheritance, as in C # or Java, I can inherit this abstract class as follows:

//TypeScript
class B extends A{
}

But in terms of implementation, you can also implement a class in TypeScript:

class C implements A {
    m(): void { }
}

So here comes the question: What is the difference in behavior between class B and class C? How do I choose?

Q & A

The implements keyword treats class A as an interface, which means class C must implement all the methods defined in A, whether or not these methods have default implementations in class A. At the same time, there is no need to define super methods in class C.

And just like the meaning of the extends keyword itself, you only need to implement the virtual method defined in class A, and the super call will also be valid.

I think in the case of abstract methods, this makes no difference. But there are very few classes that only use abstract methods. If only abstract methods are used, it is best to convert them to interfaces.

 

Reprinted at: https://www.cnblogs.com/pangjianxin/p/10901115.html

We know that a class can be implemented or extended in TypeScript. Some C # or java based students may be confused about this, because in the above two object-oriented languages, only interfaces can be implemented, but only classes In order to be extendeds. Let's explain how these two keywords differ in TypeScript. I recently found this problem in StackOverflow, so I translated it and recorded it. The original address is: https://stackoverflow.com/questions/35990538/extending-vs-implementing-a-pure-abstract-class- in-typescript / 35990799 # 35990799

Problem Description

Extending vs. implementing a pure abstract class in TypeScript

Suppose I have a clean abstract class A:

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

In terms of inheritance, as in C # or Java, I can inherit this abstract class as follows:

//TypeScript
class B extends A{
}

But in terms of implementation, you can also implement a class in TypeScript:

class C implements A {
    m(): void { }
}

So here comes the question: What is the difference in behavior between class B and class C? How do I choose?

Q & A

The implements keyword treats class A as an interface, which means class C must implement all the methods defined in A, whether or not these methods have default implementations in class A. At the same time, there is no need to define super methods in class C.

And just like the meaning of the extends keyword itself, you only need to implement the virtual method defined in class A, and the super call will also be valid.

I think in the case of abstract methods, this makes no difference. But there are very few classes that only use abstract methods. If only abstract methods are used, it is best to convert them to interfaces.

 

Guess you like

Origin www.cnblogs.com/Ewarm/p/12690917.html