Java:一个类可以同时继承两个超类吗?

发布于 2024-10-10 22:19:40 字数 121 浏览 6 评论 0原文

我有一个班级旅程,我想将其制作为超级班级和另一个班级计划的旅程。 plannedjourney 类扩展了 JFrame,因为它包含表单..但是我也希望这个类扩展 Journey..

有没有可能的方法来做到这一点?

I have a class Journey which I want to make a superclass and another class plannedjourney.
The plannedjourney class extends JFrame since it contains forms..However I also want this class to extends Journey..

Is there a possible way to do this?

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

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

发布评论

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

评论(10

忆悲凉 2024-10-17 22:19:41

不要混合模型和视图。如果您将两个域明确分开,那么您将不需要多重继承(这次)。

您有一个旅程模型类和此类旅程的查看器。查看器应该子类化 Component 并显示在窗口(JFrame 实例)中(某处)。

该查看器采用一个旅程对象并呈现该旅程的属性。 Journey 单独拥有有关旅程的所有信息,只有查看者知道如何向用户呈现旅程:

 public class PlannedJourney extends JPanel {
   private Journey journey;
   public JourneyViewer(Journey journey) {
     this.journey = journey;

     // build this panel based on the journey object
     // (add labels, subpanels, widgets, ...)
   }
 }

现在您可以创建 PlannedJourney 的实例并将其添加到应用程序 JFrame 在适当的位置。

Don't mix models and views. If you keep both domains clearly separated, then you won't be in need of multiple inheritance (this time).

You have one model class for journeys and a viewer for such journeys. The viewer should subclass Component and be displayed (somewhere) in your window (a JFrame instance).

This viewer takes one journey object and presents the journey's properties. Journey alone has all information about a journey, only the viewer knows how to present a journey to the user:

 public class PlannedJourney extends JPanel {
   private Journey journey;
   public JourneyViewer(Journey journey) {
     this.journey = journey;

     // build this panel based on the journey object
     // (add labels, subpanels, widgets, ...)
   }
 }

Now you can create an instance of PlannedJourney and add it to the applications JFrame at the appropriate position.

唠甜嗑 2024-10-17 22:19:41

Java 中没有多重继承。
这是语言所不允许的。检查是否可以更改设计以使用界面。 (这几乎总是可能的)。

No multiple inheritance in Java.
It's not allowed by the language. Check if you can change your design to use Interface instead. (it's almost always possible).

百变从容 2024-10-17 22:19:41

不,这对于 Java 来说是不可能的。通常,使用接口可以实现类似的效果(我认为 Journey 可能是这里的一个接口),但如果您希望继承方法实现,那么您将不得不重新考虑您的设计。

根据您的描述,我的最佳猜测是,您可以将类分为一个表示数据模型的层次结构和另一个扩展 JFrame 以显示数据的层次结构。

No this is not possible with Java. Usually, a similar effect can be achieved using interfaces (I'm thinking Journey may be an interface here), though if you wish to inherit method implementations, then you'll have to rethink your design.

My best guess from your description is that you can separate your classes into one hierarchy that represents the data model, and another that extends JFrame to display the data.

草莓酥 2024-10-17 22:19:41

继承页面Sun Java 教程

定义:派生类
来自另一个类的称为
子类(也是派生类,
扩展班,或儿童班)。这
子类所属的类
派生类称为超类(也称为超类)
基类或父类)。

除了对象,它没有
超类,每个类都有一个并且
只有一个直接超类(单个
继承)。在没有任何
其他显式超类,每个类
隐式是 Object 的子类。

类可以从类派生
派生自以下类
从类派生,等等,以及
最终源自最顶层
类,对象。据说这样的一个类
是所有阶级的后裔
向后延伸的继承链
对象。

From the Inheritance page of the Sun Java Tutorial

Definitions: A class that is derived
from another class is called a
subclass (also a derived class,
extended class, or child class). The
class from which the subclass is
derived is called a superclass (also a
base class or a parent class).

Excepting Object, which has no
superclass, every class has one and
only one direct superclass (single
inheritance)
. In the absence of any
other explicit superclass, every class
is implicitly a subclass of Object.

Classes can be derived from classes
that are derived from classes that are
derived from classes, and so on, and
ultimately derived from the topmost
class, Object. Such a class is said to
be descended from all the classes in
the inheritance chain stretching back
to Object.

怪异←思 2024-10-17 22:19:41

要么使 Journey 扩展 JFrame,要么使 Journey 成为一个接口。

选择对您的对象最有意义的选项结构。

如果 Journey 扩展 JFrame 有意义,那么就这样做。这样,当 PlannedJourney 扩展 Journey 时,它也会从 JFrame 继承 Journey 所做的一切。

如果这没有意义,那么将 Journey 设为一个接口,并让 PlannedJourney 实现 JourneyPlannedJourney 将无法从 Journey 继承任何代码,但您将能够继承方法签名并将对象作为 Journey 传递除了作为 JFrame 之外。


确保在使用 Swing 时将模型与视图分开。您的数据应存储在一组对象 (Model) 中,而显示该数据的 Swing 组件应存储在一组单独的对象 (View) 中。这有助于封装数据功能,将它们与 GUI 对象分开,并强制视图模型的单向依赖。考虑一下 Journey 对象可能需要 JourneyFrame 来显示。这样,JourneyFrame 就可以只负责显示它,并且可以扩展 JFrame 并仅携带对 Journey 的引用,该引用可以有自己的等级制度。

Either make Journey extend JFrame or make Journey an interface.

Choose the option that makes the most sense for your object structure.

If it makes sense for Journey to extend JFrame then do that. This way, when PlannedJourney extends Journey, it also inherits everything that Journey does from JFrame.

If this doesn't make sense, then make Journey an interface, and have PlannedJourney implement Journey. PlannedJourney will not be able to inherit any code from Journey, but you will be able to inherit method signatures and pass the object around as a Journey in addition to as a JFrame.


Be sure when you're working with Swing that you are separating out the Model from the View. Your data should be stored in one set of objects (Model), and the Swing components to display that data should be a separate set of objects (View). This helps to encapsulate data functions, separate them from GUI objects, and enforce a one-way reliance of the View on the Model. Consider that a Journey object should perhaps require a JourneyFrame to display. This way, the JourneyFrame can take care of just displaying it and can extend JFrame and carry only a reference to Journey, which can have its own hierarchy.

巷子口的你 2024-10-17 22:19:41

不。Java 中没有多重继承。这是一个经过深思熟虑的设计决定。

如果您可以找到一种方法将其中一个超类表示为接口,那么这可能会解决您的问题。

No. There is no multiple inheritance in Java. This was a deliberate design decision.

If you can find a way to express one of the superclasses as an interface instead, then this might solve your problem.

长亭外,古道边 2024-10-17 22:19:41

我认为更好的方法可能是让 PlannedJourney 扩展 Journey,然后让 PlannedJourney 拥有 JFrame 的实例。这看起来像是一种一石二鸟的方法,具体取决于你如何看待它。

I think a better way might be to have PlannedJourney extend Journey and then have PlannedJourney have an instance of JFrame. It looks like a way to kill two birds with one stone depending on how you view it.

池予 2024-10-17 22:19:41

Java 中的多重继承是不可能的。

接口的多个实现是可能的,这是最接近 Java 中的多重继承的东西。

如果可能的话,优先考虑组合而不是继承。引用自Effective Java。

Multiple inheritance in Java isn't possible.

Multiple implementations of interfaces is possible, this is the closest thing to multiple inheritance in Java you've got.

If possible, favor composition over inheritance. Quote taken from Effective Java.

醉南桥 2024-10-17 22:19:41

如果您还没有阅读 Joshua Bloch 所著的《Effective Java》,那么现在是时候读一读了。特别是“优先考虑组合而不是继承”部分。由于您无法在 Java 中进行多重继承,他解释了为什么在几乎所有情况下,正确的方法是让您的 JFrame 扩展包含对其 UI 所在的 PlannedJourney 的引用。

If you haven't read "Effective Java" by Joshua Bloch now is the time to do it. Particularly the section "favor composition over inheritance". Since you can't do multiple inheritence in Java, he explains why in almost all cases the right approach is to have your JFrame extension CONTAIN a reference to the PlannedJourney it is the UI for.

征棹 2024-10-17 22:19:41

不太确定你想要什么,但多重继承在 Java 中是非常可能且常见的事情。首先,当一个类继承另一个类时,如果该类也是另一个类的子类,它将把继承的方法传递给它的子类。其次,在 Java 中,有一个编码字符串,也称为“接口”,这允许一个类继承多个类,而无需扩展一个类。

Not exactly sure what you want but multiple inheritance is a very possible and common thing in Java. Firstly when a class inherits from another class, if that class is also a subclass of another class it will pass on it's inherited methods to it's subclasses. Secondly, within Java there is an string of coding otherwise known as 'the Interface', this allows for a class to inherit from multiple classes without extending a class.

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