java两个类相同属性值赋值

    public static void main(String[] args) {
        Student student = new Student();
        student.setName("我是谁");
        student.setAge(99);
        Student2 student1 = new Student2();
        setTargetFromSource(student1 , student);
        System.out.println(student1.toString());
    }


    /**
   	 * 第一种方法
     * @param target 需要赋值的对象
     * @param source 数据源
     */
    public static void setTargetFromSource(Object target, Object source) {

        if (target == null || source == null) {
            return;
        }

        Class classTarget = target.getClass();
        Class classSource = source.getClass();

        Field[] fieldTarget = classTarget.getDeclaredFields();
        Field[] fieldSource = classSource.getDeclaredFields();
        try {
            for (int i = 0; i < fieldTarget.length; i++) {
                for (int j = 0; j < fieldSource.length; j++) {
                    if (fieldTarget[i].getName().equals(fieldSource[j].getName()) &&
                            fieldTarget[i].getGenericType().toString().equals(fieldSource[i].getGenericType().toString())) {
                        fieldTarget[i].setAccessible(true);
                        fieldSource[j].setAccessible(true);
                        fieldTarget[j].set(target, fieldSource[i].get(source));
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 	/** 
 	 * 第二种方法
     * @param target 需要赋值的对象
     * @param source 数据源
     */
	public static void setTargetFromSource1(Object target, Object source) {
        if (target == null || source == null) {
            return;
        }

        Map<String, Field> mapField = new HashMap<>();

        Class classTarget = target.getClass();
        Class classSource = source.getClass();

        Field[] fieldTarget = classTarget.getDeclaredFields();
        Field[] fieldSource = classSource.getDeclaredFields();

        try {
            for (int i = 0; i < fieldSource.length; i++) {
                mapField.put(fieldSource[i].getName(), fieldSource[i]);
            }
            for (int i = 0; i < fieldTarget.length; i++) {
                if (mapField.containsKey(fieldTarget[i].getName()) &&
                        fieldTarget[i].getGenericType().toString().equals(mapField.get(fieldTarget[i].getName()).getGenericType().toString())) {
                    fieldTarget[i].setAccessible(true);
                    mapField.get(fieldTarget[i].getName()).setAccessible(true);
                    fieldTarget[i].set(target, mapField.get(fieldTarget[i].getName()).get(source));
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,我们可以使用多种方式将一个实体赋给另一个不同的实体。 一种常见的方式是使用构造函数。我们可以在目标实体中定义一个带有源实体类型的构造函数,并通过参数传递源实体的对象。然后,在构造函数内部,将源实体对象的属性赋给目标实体的相应属性。这样,我们就可以通过创建目标实体的对象,将源实体属性赋给目标实体的对象。 另一种常见的方式是使用setter方法。我们可以在目标实体中定义与源实体属性相同的setter方法。然后,通过获取源实体的对象和目标实体的对象,使用源实体的getter方法获取属性,并通过目标实体的setter方法将赋给目标实体属性。 以下是一个示例代码,演示了如何将一个Student属性赋给一个Person的对象: ``` public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } } public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } } public class Main { public static void main(String[] args) { Student student = new Student("John", 20); Person person = new Person(); person.setName(student.getName()); person.setAge(student.getAge()); System.out.println("Person name: " + person.getName()); System.out.println("Person age: " + person.getAge()); } } ``` 在上述示例中,我们创建了一个Student和一个Person。然后,我们实例化一个Student对象,并将其name和age属性赋给一个Person对象。最后,我们输出Person对象的name和age属性,验证赋值是否成功。 以上就是将两个不同的实体进行赋值的一种常见方式。当然,具体的实现方式可能因实际需求而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值