6

假设我有以下代码

public class SuperClass {
    protected int super_class_value;

    public SuperClass (int value) {
        this.super_class_value = value;
    }
}

public class Subclass extends SuperClass {
    protected int subclass_value;

    public SubClass(int super_class_value, int subclass_value) {
        super(super_class_value);
        this.subclass_value = subclass_value;
    }
}

但是,现在我希望能够将 SuperClass 对象传递给 SubClass 构造函数。我该怎么做?

public SubClass(SuperClass super_class, int subclass_value) {
    //What do I do here?
    this.subclass_value = subclass_value;
}

基本上,我想做这样的事情......

public SubClass(SuperClass super_calss, int subclass_value) {
    super(super_class.super_class_value);
    this.subclass_value = subclass_value;
}

但是如果 SuperClass 更复杂,我不想将每个值都添加到 super() 调用中。相反,我想简单地传入一个已经存在的对象,并将其用作超类。

我想做这个...

public SubClass(SuperClass super_class, int subclass_value) {
    super(super_class);
    this.subclass_value = subclass_value;
}

但我不确定这是否允许?


如果我将构造函数添加到 SuperClass 定义中,似乎我可以执行上述操作。但那会是什么样子?

public class SuperClass {
    protected int super_class_value;

    public SuperClass (int value) {
        this.super_class_value = value;
    }

    public SuperClass (SuperClass super_class_object) {
        //What is done here?
    }
}
4

5 回答 5

3

你不想为你的超类创建一个拷贝构造函数吗?

public class SuperClass {
    protected int super_class_value;

    public SuperClass (int value) {
        this.super_class_value = value;
    }
    protected SuperClass (SuperClass super_class) {
        this.super_class_value = super_class.supper_class_value;
    }
}

public class Subclass extends SuperClass {
    protected int subclass_value;

    public SubClass(SuperClass super_class, int subclass_value) {
        super(super_class);
        this.subclass_value = subclass_value;
    }
}

另请参阅此处关于编写复制 ctor 的问题:

Java 是否有默认的复制构造函数(如在 C++ 中)?

于 2015-02-11T20:18:00.503 回答
2

但是,现在我希望能够将 SuperClass 对象传递给 SubClass 构造函数。我该怎么做?

你在做同样的事情

public SubClass(SuperClass super_class, int subclass_value) {
    //What do I do here?
    // Do Whatever you want to do here ,
    this.subclass_value = subclas_value;
}

现在我想您可能正在考虑如何将对象从子类传递给超类,那么这可以通过super keyword在 Java中完成

  • super()用于调用直接父类构造函数。

现在要将 Object 传递给 SuperClass ,在您的子类的构造函数中,您的第一行必须是

super(super_class);

现在在你的超类中,你必须有一个构造函数,它本身带有 Object 否则上面的行会抛出编译时错误

public SuperClass (SuperClass object) {
         // Do whatever you want to do .
          this.anyInstanceVariable=object.thatInstanceVariable;
    }

PS:我仍然不明白你想用这个实现什么,但无论你提到什么,这都是你实现它的方式


更新

正如您在评论中所说,您不想复制每个单个变量,那么您可以使用克隆方法Object class

这是一个示例,您将如何在您的情况下实现它

class SuperClass implements Cloneable{ // otherwise clone() method will throw CloneNotSupportedException

    int a;
    SuperClass obj=null; // this will point to the copy of the cloned object

    SuperClass(int a){// just to test that cloning is successfull
        this.a=a;
    }
    SuperClass(SuperClass a){
        try{
        this.obj=(SuperClass)a.clone();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        display();
    }
    public void display(){// to Verify that Cloning has been done successfully
        try{
        System.out.println("Hello from cloned Object "+obj.a);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    @Override
    protected Object clone() throws CloneNotSupportedException  { // Overridden Method
        return super.clone();
    }
}

查看这篇关于java 中的对象克隆的好文章

于 2015-02-11T20:11:30.257 回答
0

Check out the Decorator Pattern for a well-known example of this. The page includes a good java example with UML diagram too.

于 2015-02-11T20:08:05.890 回答
0
public class SuperClass {
    protected int super_class_value;

    public SuperClass (int value) {
        this.super_class_value = value;
    }

    Object obj = new Object();
}

public class Subclass extends SuperClass {
    protected int subclass_value;

    private SuperClass superCl = new SuperClass();

    public SubClass(int super_class_value, Object obj2 ,int subclass_value) {
        super(super_class_value);
        obj2 = superCl.obj;
        this.subclass_value = subclass_value;
    }
}
于 2015-02-11T20:08:57.927 回答
0

您可以在其中添加 getter 方法并在构造函数SuperClass中使用它们。SubClass

public class SuperClass {
    protected int super_class_value;

    public SuperClass (int value) {
        this.super_class_value = value;
    }

    public int getSuperClassValue(){ return super_class_value;}
}

public class Subclass extends SuperClass {
    protected int subclass_value;

    public SubClass(SuperClass super_class, int subclass_value) {
        super(super_class.getSuperClassValue());
        this.subclass_value = subclass_value;
    }
}
于 2015-02-11T20:01:42.620 回答