Assign the properties of the parent class to the child class (using reflection)

public class A {
    private String a;
    private String b;

    public String getA() {
        return a;
    }

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

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }
}
public class B extends A {
    private String c;
    private String d;

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

    public String getD() {
        return d;
    }

    public void setD(String d) {
        this.d = d;
    }
}

If there are two entity classes, class A and class b, and class b inherits from Class A, now I want to assign the A and b attributes in class A to the A and b attributes in class b. the first method I tried was to convert class A to class b. This is a wrong idea, because in inheritance, it can't be turned down. For example, if there are saxophone and violin in an instrument, you can say that the violin is an instrument, But we can't say that an instrument is a violin. Then we look up the data and know that reflection can get all kinds of information of a class at runtime. So the idea is to pass the parent class and subclass that we want to transform into a method, and then use reflection to get the parameter name and value in the parent class (at this time, we want to get the value by calling the get parameter name method, for example, There is an msg parameter. Only when there is a getMsg method in the entity class). After getting it out, you can assign the value to the incoming subclass

Here is the source code

/**
 * @program: demo
 * @description: reflex
 * @author: hu_pf@suixingpay.com
 * @create: 2018-03-29 16:02
 **/
public class Demo11 {
    public static void main(String[] args) throws Exception {
        A a=new A();
        B b=new B();
        a.setA("a");
        a.setB("b");
        fatherToChild(a,b);
        System.out.println(b.getA());
    }


    public static <T>void fatherToChild(T father,T child) throws Exception {
        if (child.getClass().getSuperclass()!=father.getClass()){
            throw new Exception("child No father Subclasses");
        }
        Class<?> fatherClass = father.getClass();
        Field[] declaredFields = fatherClass.getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            Field field=declaredFields[i];
            Method method=fatherClass.getDeclaredMethod("get"+upperHeadChar(field.getName()));
            Object obj = method.invoke(father);
            field.setAccessible(true);
            field.set(child,obj);
        }

    }

    /**
     * Initial capital, in:deleteDate, out:DeleteDate
     */
    public static String upperHeadChar(String in) {
        String head = in.substring(0, 1);
        String out = head.toUpperCase() + in.substring(1, in.length());
        return out;
    }
}

Posted by DonelleJenae on Sun, 05 Apr 2020 18:33:02 -0700