java反射的实际应用

java反射覆盖

    功能:用于某个对象的新旧值进行更新覆盖操作。用途:常用于电商系统订单以及商品的新旧更新。

具体代码:下面以学生实体类进行讲解。

/**
 * 学生类
 */
public class Student {
    private Integer sid;
    private String sname;
    private Integer age;
    private String gender;

    public Student(int sid, String sname, int age, String gender) {
        this.sid = sid;
        this.sname = sname;
        this.age = age;
        this.gender = gender;
    }



    public Student(String sname, Integer age, String gender) {
        this.sname = sname;
        this.age = age;
        this.gender = gender;
    }

    public Student() {
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public Integer getAge() {
        return age;
    }

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

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }



    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}

 

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 反射工具类
 */
public class ReflectUtils {

    /**
     * 反射覆盖
     *
     */
    public static void saveOrupdate(Object s1,Object s2) throws  Exception{
        Class<?> c1=s1.getClass();
        Class<?> c2=s2.getClass();
        Field[] fields=c1.getDeclaredFields();
        for(Field field:fields) {
            field.setAccessible(true);
            String name = field.getName(); // 获取属性的名字
            name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写,方便构造get,set方法
            String type = field.getGenericType().toString(); // 获取属性的类型
            if (type.equals("class java.lang.String")) { // 如果type是类类型,则前面包含"class ",后面跟类名
                Method m = c2.getMethod("get" + name);
                String value = (String) m.invoke(s2); // 调用getter方法获取属性值
                field.set(s1,value);
            }
            if (type.equals("class java.lang.Integer")) {
                Method m = c2.getMethod("get" + name);
                Integer value = (Integer) m.invoke(s2);
                field.set(s1,value);
            }

            if (type.equals("class java.lang.Boolean")) {
                Method m = c2.getMethod("get" + name);
                Boolean value = (Boolean) m.invoke(s2);
                field.set(s1,value);
            }
            if (type.equals("class java.util.Date")) {
                Method m = c2.getMethod("get" + name);
                Date value = (Date) m.invoke(s2);
                field.set(s1,value);
            }
        }

    }

    public static void main(String[] args) {
        Student s1=new Student(1,"张三",24,"男");
        Student s2=new Student("赵薇",38,"女");
        try {
            saveOrupdate(s1,s2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(s1.toString());

    }
}

 

posted @ 2019-09-04 10:45  皓月苍狼  阅读(1342)  评论(0编辑  收藏  举报