简体   繁体   English

一个抽象类可以扩展另一个抽象类并增加功能

[英]Can one abstract class extend another abstract class and increase functionality

I have an abstract class. 我有一个抽象的课。 I want to extend the abstract class by another abstract class and then implement the extended abstract class. 我想通过另一个抽象类扩展抽象类,然后实现扩展的抽象类。 Is it possible .If yes, whether it's a good approach in point of view regarding OOPS? 是否可能。如果是的话,对于OOPS来说,这是否是一个好的方法?

I'm not sure about Java in particular, but it should be valid. 我不太确定Java,但它应该是有效的。

In terms of OOP, if it makes sense, then run with it. 就OOP而言,如果它有意义,那么运行它。 To use some old examples, you might have a Vehicle abstract class and then LandVehicle and FlyingVehicle abstract classes. 要使用一些旧的示例,您可能有一个Vehicle抽象类,然后是LandVehicleFlyingVehicle抽象类。 As long as your example makes sense as an abstract class, then you should be fine. 只要您的示例作为抽象类有意义,那么您应该没问题。

Yes, it is possible, and I don't see a reason not to use it if you need it (disclaimer: but however there are many ways to misuse this, and to over-complicate things, as with everything in programming usually). 是的,这是可能的,如果你需要,我没有理由不使用它(免责声明:但是有很多方法可以滥用它,并且过于复杂化,就像编程中的所有内容一样)。

One thing to notice is that the second abstract class doesn't need to implement abstract methods from first class, but the first concrete must implement both. 需要注意的是,第二个抽象类不需要从第一个类实现抽象方法,但第一个具体必须实现它们。

Yes, you can! 是的你可以! One abstract class can be extended by another abstract class 一个抽象类可以由另一个抽象类扩展

Yes! 是!

But it makes sense only if the abstract subclass adds more functionality (abstract or not). 但只有抽象子类添加更多功能(抽象或不抽象)才有意义。

Otherwise, I don't see the point. 否则,我没有看到这一点。

Yes you can do it. 是的,你可以做到。 And it is good practice if your child class adds more functionality. 如果您的子类增加了更多功能,那么这是一种很好的做法。 It allows to move toward specification. 它允许转向规范。 Your parent class becomes a more general class and child class a more specific one. 您的父类成为更通用的类,子类更具体。 And you can implement both as per your requirement. 您可以根据自己的要求实施两者。

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

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