将超类对象作为参数传递给子类构造函数(java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34817489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:24:42  来源:igfitidea点击:

Passing superclass object as parameter to subclass constructor (java)

javainheritance

提问by Machtyn

I've done a bit of searching, but I'm either not asking the right question or not remembering correctly. In any case, in Java, I am wondering if it is possible to pass the super class object in as a parameter to a subclass and what the most efficient way of making that object's data available to the class's super class.

我已经做了一些搜索,但我要么没有问正确的问题,要么没有正确记住。无论如何,在 Java 中,我想知道是否可以将超类对象作为参数传递给子类,以及使该对象的数据可用于类的超类的最有效方法是什么。

Code examples:

代码示例:

public class superclass {
  String myparm1;
  String myParm2;
  int myParmN;

  public superclass(String p1, String p2, int pn)
  {
    this.myparm1 = p1;
    this.myparm2 = p2;
    this.myParmN = pn;
  }
  // other methods here
}

public class subclass extends superclass {
  double b1;
  double b2;

  public subclass(superclass sc, double b1, double b2) {
    // easy way to make sc data available to this class?
    // Do I create a copy or clone method, or some other way?
    // calling super(sc); wouldn't exactly work 
    this.b1 = b1;
    this.b2 = b2;
  }
}

if I had a constructor in superclass that was public superclass(superclass sc) { // assign sc properties to this properties, correct? }then I could simply use super(sc);

如果我在超类中有一个构造函数,public superclass(superclass sc) { // assign sc properties to this properties, correct? }那么我可以简单地使用super(sc);

采纳答案by Jim W

There's no point to passing in a reference to the superclass of an object in the constructor. Your subclass is already an instance of the superclass.

在构造函数中传入对对象超类的引用是没有意义的。您的子类已经是超类的一个实例。

Even though you can't directly see the private components of the superclass, but they still exist and calls to public accessor methods will still produce normal behavior.

即使您不能直接看到超类的私有组件,但它们仍然存在并且对公共访问器方法的调用仍然会产生正常的行为。

In answer to your second question, the most efficient way to access the data inside the parent class is with the accessor methods of that parent class. If it has get/set properties methods that populate some data structure full of properties, just call those methods from your child class and they'll work exactly the same as they did for the parent. Now, if those internal data structures are populated by the constructor of the parent class, you'll have to invoke that constructor with the correct methods when you create an instance of the child constructor that needs them- typically by calling the appropriate super() at the beginning of the child's constructor.

在回答您的第二个问题时,访问父类内部数据的最有效方法是使用该父类的访问器方法。如果它具有填充一些充满属性的数据结构的 get/set 属性方法,只需从您的子类中调用这些方法,它们的工作方式将与它们为父类所做的完全相同。现在,如果这些内部数据结构由父类的构造函数填充,则在创建需要它们的子构造函数的实例时,您必须使用正确的方法调用该构造函数 - 通常通过调用适当的 super()在孩子的构造函数的开头。

If you're trying to get around the restriction that you can't see the private parts of the superclass, java intentionally doesn't let you do that. You can get around this with reflection unless you're stuck inside an execution environment that disallows this, but I generally wouldn't consider this a safe or elegant approach.

如果您试图绕过无法看到超类的私有部分的限制,java 故意不允许您这样做。您可以通过反射来解决这个问题,除非您被困在不允许这样做的执行环境中,但我通常不会认为这是一种安全或优雅的方法。

From comment below, I understand what the OP is trying to do and this should work, though obviously it depends upon your ability to make changes to the super class:

从下面的评论中,我了解 OP 正在尝试做什么并且这应该可以工作,但显然这取决于您对超类进行更改的能力:

public class Super
{
    public Super (Super other)
    {
        //copy stuff from other to this
    }
}

public class Child extends Super
{
    public Child (Super other)
    {
        super(other);
        //continue constructor
    }

}

回答by Raffaele

You can't. When you build an object with Java (for example with new), that instance has a class, and that class has a parent (possibly Object).

你不能。当您使用 Java 构建对象时(例如使用new),该实例具有一个类,并且该类具有一个父类(可能是 Object)。

The parent-child relation only holds between classes, not between objects! No object has a parent object(at least, not at the language level) - so you can't accept the parent in the contructor and store it in some Java-defined place.

父子关系只存在于类之间,而不存在于对象之间!没有对象有父对象(至少在语言级别没有) - 所以你不能在构造函数中接受父对象并将其存储在某个 Java 定义的地方。

However your domain can have parent and child entities, in which case you need fields or data structures to store the links between them.

然而,您的域可以有父实体和子实体,在这种情况下,您需要字段或数据结构来存储它们之间的链接。

回答by Machtyn

To answer a previous question, suppose the superclass has large number of properties. In that case, the design of the class may be bad, of course.

要回答前面的问题,假设超类具有大量属性。在那种情况下,类的设计当然可能很糟糕。

Perhaps the best answer is:

也许最好的答案是:

public class superclass {
  // properties and constructors as defined in OP

  public void copy(superclass sc) {
    this.myParm1 = sc.getMyParm1();
    this.myParm2 = sc.getMyParm2();
    this.myParmN = sc.getMyParmN();
  }

  // other methods as needed
}

That way, the sub class can just call the super.copy(sc). Of course, I would need another constructor in superclass that will set defaults: public superclass() { // set defaults }

这样,子类就可以调用super.copy(sc). 当然,我需要超类中的另一个构造函数来设置默认值:public superclass() { // set defaults }

So subclass could be:

所以子类可以是:

public class subclass extends superclass {
  //properties as defined in OP

  public subclass(superclass sc, double b1, double b2) {
    this.b1 = b1;
    this.b2 = b2;
    super.copy(sc);
  }
}

In this way, I'm only having to type those parameters out, and any subclasses that would want to accept a superclass object won't have to define that structure each and every time. (less typing, less chance for mistake or forgetting something.)

通过这种方式,我只需要输入这些参数,并且任何想要接受超类对象的子类都不必每次都定义该结构。(减少打字,减少出错或忘记某事的机会。)