简体   繁体   English

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

[英]Java: Can a class inherit from two super classes at the same time?

I have a class Journey which I want to make a superclass and another class plannedjourney. 我有一个课程Journey,我想创建一个超级课程和另一个课程计划之旅。 The plannedjourney class extends JFrame since it contains forms..However I also want this class to extends Journey.. 计划的旅程类扩展了JFrame,因为它包含表单..但是我也希望这个类扩展Journey ..

Is there a possible way to do this? 有可能这样做吗?

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). 查看器应该子类化Component并在窗口中显示(某处)( JFrame实例)。

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: 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, ...)
   }
 }

Now you can create an instance of PlannedJourney and add it to the applications JFrame at the appropriate position. 现在,您可以创建PlannedJourney的实例,并将其添加到适当位置的应用程序JFrame

No multiple inheritance in Java. Java中没有多重继承。 It's not allowed by the language. 语言不允许这样做。 Check if you can change your design to use Interface instead. 检查您是否可以将设计更改为使用Interface。 (it's almost always possible). (这几乎总是可能的)。

No this is not possible with Java. 不,这不可能用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. 通常,使用接口可以实现类似的效果(我认为Journey可能是这里的接口),但是如果您希望继承方法实现,那么您将不得不重新考虑您的设计。

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. 我从你的描述中得到的最好的猜测是,你可以将你的类分成一个代表数据模型的层次结构,另一个扩展JFrame来显示数据。

From the Inheritance page of the Sun Java Tutorial 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) . Excepting Object,没有超类, 每个类都有一个且只有一个直接超类(单继承) In the absence of any other explicit superclass, every class is implicitly a subclass of Object. 在没有任何其他显式超类的情况下,每个类都隐式地是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. 类可以从派生自类的类派生的类派生,依此类推,最终派生自最顶层的类Object。 Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object. 据说这样的类是继承链中延伸回Object的所有类的后代。

Either make Journey extend JFrame or make Journey an interface. Journey扩展JFrame或使Journey成为一个界面。

Choose the option that makes the most sense for your object structure. 选择对您的对象结构最有意义的选项。

If it makes sense for Journey to extend JFrame then do that. 如果Journey扩展JFrame是有意义的,那么就这样做。 This way, when PlannedJourney extends Journey , it also inherits everything that Journey does from JFrame . 这样,当PlannedJourney扩展Journey ,它还继承了JourneyJFrame所做的一切。

If this doesn't make sense, then make Journey an interface, and have PlannedJourney implement Journey . 如果这没有意义,那么让Journey成为一个界面,并让PlannedJourney实现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 . PlannedJourney将无法继承Journey任何代码,但除了作为JFrame之外,您还可以继承方法签名并将对象作为Journey传递。


Be sure when you're working with Swing that you are separating out the Model from the View . 确保在使用Swing时,您正在从视图中分离出模型 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 ). 您的数据应存储在一组对象( Model )中,而Swing组件应显示该数据应该是一组单独的对象( View )。 This helps to encapsulate data functions, separate them from GUI objects, and enforce a one-way reliance of the View on the Model . 这有助于封装数据函数,将它们与GUI对象分离,并强制对模型上的View进行单向依赖。 Consider that a Journey object should perhaps require a JourneyFrame to display. 考虑到Journey对象应该要求显示JourneyFrame 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. 通过这种方式, JourneyFrame可以只显示它,并且可以扩展JFrame并仅带有对Journey的引用, Journey可以有自己的层次结构。

No. There is no multiple inheritance in Java. 不。在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. 如果您可以找到一种方法来表示其中一个超类作为接口,那么这可能会解决您的问题。

I think a better way might be to have PlannedJourney extend Journey and then have PlannedJourney have an instance of JFrame. 我认为更好的方法可能是让PlannedJourney扩展Journey,然后让PlannedJourney拥有一个JFrame实例。 It looks like a way to kill two birds with one stone depending on how you view it. 它看起来像一种方法,一石二鸟,取决于你如何看待它。

Multiple inheritance in Java isn't possible. Java中的多重继承是不可能的。

Multiple implementations of interfaces is possible, this is the closest thing to multiple inheritance in Java you've got. 接口的多个实现是可能的,这是Java中最接近多重继承的东西。

If possible, favor composition over inheritance. 如果可能的话,赞成组合而不是继承。 Quote taken from Effective Java. 引自取自Effective Java。

If you haven't read "Effective Java" by Joshua Bloch now is the time to do it. 如果你还没有读过Joshua Bloch的“Effective Java”,现在是时候了。 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中做多重继承,他解释了为什么在几乎所有情况下,正确的方法是让你的JFrame扩展包含对PlannedJourney的引用,它是用户界面。

Not exactly sure what you want but multiple inheritance is a very possible and common thing in Java. 不完全确定你想要什么,但多重继承是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. 其次,在Java中有一串编码,也称为“接口”,这允许类从多个类继承而不扩展类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 中的通配符,用于从相同 class 继承的类 - WildCard in Java for classes that inherit from same class 从超类继承字段或在子类中写入字段? - Inherit fields from super class or write fields in sub classes? Java:d​​o方法是否继承了JRE超类,就像继承Object一样? - Java: do methods inherit a JRE super class, like classes inherit Object? 如何在Java中从超级类继承子类的构造方法 - how to inherit Part of Constructor from super class to sub class in java Java方法仅采用继承自超类的对象 - Java method that takes only objects that inherit from super class 在java中,内部类是否可以从内部类外部类之外定义的抽象类继承? - In java can inner classes inherit from an abstract class defined outside of the inner class's outer class? java - 当子类和超类在不同的包中时,如何继承java中的类 - How do I inherit a class in java, when the sub- and super-classes are in different packages 子类是否继承超类的关联类 - Do sub classes inherit the associated classes of super class Java-您可以从超类继承并拥有一个静态构造函数来引用基类而不是父类吗? - Java - Can you inherit from a superclass and have a static constructor that references the base classes as opposed to the parent class? 从Android中的超类继承数据 - Inherit data from super class in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM