Java中用多个子类实现父类方法

发布于 2024-10-06 21:58:12 字数 276 浏览 0 评论 0原文

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

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

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

对于这个问题你有什么想法吗?

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

In each child class, there are specific methods that I'd like to be accessible from an instantiation of the parent class 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨的味道风的声音 2024-10-13 21:58:12

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文