将父类的属性赋值给子类(利用反射)

方法1

org.springframework.beans.BeanUtils.copyProperties(父类对象,子类对象);

package com.***.test11111;

//Arbor 2022/6/7
public class Dog {
    String name;
    int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


Father类

package com.***.test11111;

//Arbor 2022/6/7
public class Father {
    String name;
    int age;
    Dog dog;

    public void setAge(int age) {
        this.age = age;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public Dog getDog() {
        return dog;
    }

    @Override
    public String toString() {
        return "Father{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", dog=" + dog +
                '}';
    }
}

package com.***.test11111;

//Arbor 2022/6/7
public class Son extends Father {
    int height;

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "Son{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", dog=" + dog +
                ", height=" + height +
                '}';
    }
}

package com.***.test11111;//Arbor 2022/6/7

import org.springframework.beans.BeanUtils;

public class Test {

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.setName("小黄");
        dog.setAge(1);


        Father father = new Father();
        father.setAge(30);
        father.setName("bb");
        father.setDog(dog);

        Son son = new Son();
        son.setHeight(180);
        BeanUtils.copyProperties(father,son);

        System.out.println(son);
        System.out.println(son.getDog());
        System.out.println(son.getDog()==father.getDog());

    }
}

控制台打印:

C:\Users\Arbor\.m2\repository\commons-net\commons-net\3.8.0\commons-net-3.8.0.jar" com.***.test11111.Test
Son{name='bb', age=30, dog=com.elemis.test11111.Dog@380fb434, height=180}
com.elemis.test11111.Dog@380fb434
true

Process finished with exit code 0

方法2

public class test{
    public static void main(String[] args) throws Exception {
        A a=new A();
        a.setA("a");
        a.setB("b");
        B b=new B();
        fatherClassPropertiesToChild(a,b);
        System.out.println(b.getA());
    }


    public static <T>void fatherClassPropertiesToChild(T father,T child) throws Exception {
        if (child.getClass().getSuperclass()!=father.getClass()){
            throw new Exception("child 不是 father 的子类");
        }
        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);
        }

    }

    /**
     * 首字母大写,hello->Hello
     */
    public static String upperHeadChar(String in) {
        String head = in.substring(0, 1);
        String out = head.toUpperCase() + in.substring(1, in.length());
        return out;
    }
}

方法3

一个一个set

posted @ 2022-06-07 15:18  Arborblog  阅读(424)  评论(0编辑  收藏  举报