初始化已实例化的子类对象的超类值

时间:2017-02-22 04:56:06

标签: java inheritance

class SubClass extends SuperClass {
    int a;
    int b;
    public int getA() {
        return this.a
    }
    public int getB() {
        return this.b
    }
    public void setA(int a) {
        this.a = a
    }
    public void setB() {
        this.b = b
    }
}

class SuperClass {
    int c;
    public int getC() {
        return this.c
    }
    public void setC(int c) {
        this.c = c
    }
}

假设我已经有一个初始化SubClass对象且未初始化SuperClass个变量,我该如何初始化该SuperClass个对象的SubClass个变量。

例如:一个函数返回一个SubClass对象,a = 10b = 10c未设置(因此为0)。我有一个SuperClass对象c = 30。除了手动设置SubClass对象的每个值之外,如何将c = 30对象初始化为SuperClass

3 个答案:

答案 0 :(得分:1)

这只能通过反思来完成:

import java.lang.reflect.Field;

public class Test {
    public static void main(String[] args) throws IllegalAccessException {
        SubClass subClass = new SubClass();
        subClass.a = 10;
        subClass.b = 10;

        SuperClass superClass = new SuperClass();
        superClass.c = 30;

        for (Field field : SuperClass.class.getDeclaredFields()) {
            field.set(subClass, field.get(superClass));
        }

        System.out.println(subClass.c); // prints out 30
    }

}

class SubClass extends SuperClass {
    int a;
    int b;
}

class SuperClass {
    int c;
}

但是我建议你不要使用这种方法,只需在你的子类中实现merge(SuperClass superClass)方法:

class SubClass extends SuperClass {
    int a;
    int b;

    void merge(SuperClass superClass) {
        this.c = superClass.c;
    }
}

答案 1 :(得分:0)

您需要确定需要初始化的对象的crtical状态,以便完全创建对象。应通过参数化构造函数初始化这些状态。

对于其他州,您可以选择Builder Pattern(在创建Builder对象期间需要传递的关键状态)。您可以使用Builde类来构建对象的状态,并且一旦设置了所有状态,则方法可以为您提供对象。

public class Builder{
      int a, b, c;

      //critical states may be included as parameters and Builder class may have a constructor corresponding to the same
      public Builder getBuilder(){
             return new Builder();
      }
      public Builder setA(int a){
             this.a = a;
             return this;
     }
     public Builder setB(int b){
             this.b = b;
             return this;
     }
     public Builder setC(int c){
             this.c = c;
             return this;
     }

    public SubClass buildObject(){
         SubClass obj = new SubClass(); //in case of some critical states those can be passed as parameter and corresponding constructor should be created in the class.
       // in case a, b, c are object references, you may need to check if not null then set else dont set (let the default set by the class set them.
         obj.setA(a); 
         obj.setB(b);
         obj.setC(c);
         return obj;
    } 
}

现在,客户可以轻松创建对象。 SubClass object = new Builder().setA(10).setB(20).setC(30).buildObject();

如果您不想设置少数

SubClass object = new Builder().setA(10).setC(30).buildObject();

答案 2 :(得分:-1)

public class SubClass extends SuperClass {
    private int a;
    private int b;

    public int getA() {
        return this.a;
    }

    public int getB() {
        return this.b;
    }

    public void setA(int a) {
        this.a = a;
    }

    public void setB(int b) {
        this.b = b;
    }

    // you can do any you want with c
    public void setSuperC(int c) { 
        setC(c);
    }

}

public class SuperClass {
    private int c = 30;

    protected int getC() {
        return this.c;
    }

    protected void setC(int c) {
        this.c = c;
    }
}