我们说我有以下代码
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?
}
}
答案 0 :(得分: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;
}
现在我想你可能正在考虑如何从子类传递Object到SuperClass然后这可以通过Java中的super keyword
完成
super()
用于调用直接父类构造函数。现在将Object传递给SuperClass,在你的Subclass构造函数中,你的第一行必须是
super(super_class);
现在在你的超类中你必须有一个构造函数,它接受自身的Object,否则上面的行会抛出编译时错误
public SuperClass (SuperClass object) {
// Do whatever you want to do .
this.anyInstanceVariable=object.thatInstanceVariable;
}
P.S。 :我仍然不明白你想用这个来实现什么,但无论你提到什么,这都是你实现它的方式
<强>更新强>
正如您在评论中所说,您不想复制每个变量,那么您可以使用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();
}
}
的这篇好文章
答案 1 :(得分:2)
难道你不想只为你的超类提供copy constructor吗?
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;
}
}
另请参阅此处有关撰写复制文件的问题:
答案 2 :(得分:0)
您可以在SuperClass
中添加getter方法,并在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;
}
}
答案 3 :(得分:0)
查看Decorator Pattern以获取众所周知的示例。该页面包含一个带有UML图的好例子。
答案 4 :(得分: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;
}
}