Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

我想实现这样一个功能:两个Java子类A和B,继承自同一父类,但是它们的成员变量的值各不相同。现在想把A和B合并成C,要求:

1 当A的某成员变量不是null、B的某成员变量是null时,取A的成员变量;
2 当B的某成员变量不是null、A的某成员变量是null时,取B的成员变量;
3 当A和B的某成员变量都不是null时,姑且取B的成员变量。

比如:

class A implements Serializable {
    private int roleID = 123456;
    private String userName = "一个码农";
    private String passWord = "111";
}
class B implements Serializable {
    private int roleID = 999;
    private String userName;
    private String passWord;
}

那么合并之后应该得到:

class C {
    private int roleID = 999;
    private String userName = "一个码农";
    private String passWord = "111";
}

我写了一个比较简单的反射,虽然可以遍历所有成员变量,但是不知道该怎么合并?

    public static void combine(Object object1, Object object2) {
        Class cls1 = object1.getClass();
        Class cls2 = object2.getClass();
        Field[] fields1 = cls1.getDeclaredFields();
        Field[] fields2 = cls2.getDeclaredFields();
        for (int i = 0; i < fields1.length; i++) {
            Field f1 = fields1[i];
            Field f2 = fields2[i];
            f1.setAccessible(true);
            f2.setAccessible(true);
            try {
                // 假设f2就是合并后的C……
                if (f2.get(object) == null)
                    f2.setName(f1.get(object)) ???
                else if (f1.get(object) == null)
                    f2.setName(f2.get(object)) ???
                **???**

谢谢了先!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
621 views
Welcome To Ask or Share your Answers For Others

1 Answer

image.png

  • 简单实例你可以根据需求改一改.

import java.io.Serializable;
import java.lang.reflect.Field;

class A implements Serializable {
    private final int roleID = 123456;
    private final String userName = "一个码农";
    private final String passWord = "111";
}

class B implements Serializable {
    private final int roleID = 999;
    private String userName;
    private String passWord;
}

class Run {
    public static void main(String[] args) throws IllegalAccessException {
        A a = new A();
        B b = new B();

        Class<? extends A> ac = a.getClass();
        Class<? extends B> bc = b.getClass();
        Field[] afield = ac.getDeclaredFields();
        Field[] bfield = bc.getDeclaredFields();


        for (Field af : afield) {
            for (Field bf : bfield) {
                af.setAccessible(true);
                bf.setAccessible(true);

                // 取字段名称
                String aFieldName = af.getName();
                String bFieldName = bf.getName();

                // 取字段的数据
                Object aFieldValue = af.get(a);
                Object bFieldValue = bf.get(b);
                if (aFieldName.equals(bFieldName)) {
                    if (bFieldValue == null) {
                        // 如果为空设置

                        bf.set(b, aFieldValue);
                    }

                }

            }
        }
        System.out.println();


    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...