本文介绍了使用Java中的几个子类实现父类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类(我们称之为 A ),它由几个子类扩展( B C D 等)。

I have a class (let's call it A) that is extended by several children class (B, C, D, etc.).

在每个子类中,我都希望从父类的实例化中访问特定的方法 A

In each child class, there are specific methods that I'd like to be accessible from an instantiation of the parent class A.

我试图将 A 声明为抽象类,并将每个子类方法声明为抽象。然后我在他们自己的类中实现了这些方法,但似乎每个子类必须实现父类的每个方法。但是,我不能这样做。

I tried to declare A as an abstract class, and to declare each child class methods inside as abstract. Then I implemented these methods in their own class but it seems that each child class must implement every method of the parent class. However, I can't do this.

您对此问题有所了解吗?

Would you have an idea for this issue?

推荐答案

在每个子类中,有一些特定的方法,我希望可以从父类A的实例化。

这不是继承点。即使你能做到这一点,也表明你的设计已被打破。

That's not the point of inheritance. Even if you could do this, it shows that your design is broken.

这篇关于使用Java中的几个子类实现父类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 15:38