Java:一个类可以同时继承两个超类吗?
我有一个班级旅程,我想将其制作为超级班级和另一个班级计划的旅程。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
不要混合模型和视图。如果您将两个域明确分开,那么您将不需要多重继承(这次)。
您有一个旅程模型类和此类旅程的查看器。查看器应该子类化
Component
并显示在窗口(JFrame
实例)中(某处)。该查看器采用一个旅程对象并呈现该旅程的属性。
Journey
单独拥有有关旅程的所有信息,只有查看者知道如何向用户呈现旅程:现在您可以创建 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 (aJFrame
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:Now you can create an instance of PlannedJourney and add it to the applications
JFrame
at the appropriate position.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).
不,这对于 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.
从继承页面Sun Java 教程
From the Inheritance page of the Sun Java Tutorial
要么使
Journey
扩展JFrame
,要么使Journey
成为一个接口。选择对您的对象最有意义的选项结构。
如果
Journey
扩展JFrame
有意义,那么就这样做。这样,当PlannedJourney
扩展Journey
时,它也会从JFrame
继承Journey
所做的一切。如果这没有意义,那么将
Journey
设为一个接口,并让PlannedJourney
实现Journey
。PlannedJourney
将无法从Journey
继承任何代码,但您将能够继承方法签名并将对象作为Journey
传递除了作为JFrame
之外。确保在使用 Swing 时将模型与视图分开。您的数据应存储在一组对象 (Model) 中,而显示该数据的 Swing 组件应存储在一组单独的对象 (View) 中。这有助于封装数据功能,将它们与 GUI 对象分开,并强制视图对模型的单向依赖。考虑一下
Journey
对象可能需要JourneyFrame
来显示。这样,JourneyFrame
就可以只负责显示它,并且可以扩展JFrame
并仅携带对Journey
的引用,该引用可以有自己的等级制度。Either make
Journey
extendJFrame
or makeJourney
an interface.Choose the option that makes the most sense for your object structure.
If it makes sense for
Journey
to extendJFrame
then do that. This way, whenPlannedJourney
extendsJourney
, it also inherits everything thatJourney
does fromJFrame
.If this doesn't make sense, then make
Journey
an interface, and havePlannedJourney
implementJourney
.PlannedJourney
will not be able to inherit any code fromJourney
, but you will be able to inherit method signatures and pass the object around as aJourney
in addition to as aJFrame
.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 aJourneyFrame
to display. This way, theJourneyFrame
can take care of just displaying it and can extendJFrame
and carry only a reference toJourney
, which can have its own hierarchy.不。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.
我认为更好的方法可能是让 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.
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.
如果您还没有阅读 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.
不太确定你想要什么,但多重继承在 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.