问题描述
我有一个课程Journey,我想创建一个超级课程和另一个课程计划之旅。
计划旅程类扩展了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..
有可能这样做吗?
推荐答案
不要混合模型和视图。如果你保持两个域明确分开,那么你将不需要多重继承(这一次)。
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).
你有一个旅程的模型类和一个查看器这样的旅程。查看器应该子类 Component
并在窗口中显示(某处)( JFrame
实例)。
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, ...)
}
}
现在您可以创建PlannedJourney实例并将其添加到应用程序 JFrame
在适当的位置。
Now you can create an instance of PlannedJourney and add it to the applications JFrame
at the appropriate position.
这篇关于Java:一个类可以同时从两个超类继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!