简体   繁体   English

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

[英]Passing superclass object as parameter to subclass constructor (java)

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? } 如果我在超类中有一个public superclass(superclass sc) { // assign sc properties to this properties, correct? }的构造函数public superclass(superclass sc) { // assign sc properties to this properties, correct? } 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);

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. 如果它有get / set属性方法来填充一些充满属性的数据结构,那么只需从子类中调用这些方法,它们的工作方式与它们对父类完全相同。 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. 现在,如果这些内部数据结构由父类的构造函数填充,则在创建需要它们的子构造函数的实例时,您必须使用正确的方法调用该构造函数 - 通常通过调用相应的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. 如果你试图绕过限制,你无法看到超类的私有部分,java故意不允许你这样做。 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. 你可以通过反射解决这个问题,除非你被困在一个不允许这样做的执行环境中,但我通常不会认为这是一种安全或优雅的方法。

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
    }

}

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. 但是,您的域可以具有父实体和子实体,在这种情况下,您需要字段或数据结构来存储它们之间的链接。

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) . 这样,子类就可以调用super.copy(sc) Of course, I would need another constructor in superclass that will set defaults: public superclass() { // set defaults } 当然,我需要在超类中设置默认值的另一个构造函数: 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.) (减少打字,减少错误或遗忘的机会。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM