• 反射获取设置值


    package other;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.sql.Date;
    import java.sql.Timestamp;
    
    public class StuReflect {
    
        public static void main(String[] args) {
                 
                try { 
                    Stu stu1 = new Stu();
                    stu1.setName("lisi");
                    stu1.setFname("aaa");
                    Object object = stu1;
                    
                    method(object, "name", "get", "");
                    method(object, "name", "set", "ws");
                    method(object, "name", "get", "");
                    method(object, "fname", "get", "");
                    } catch (Exception e) {
                    e.printStackTrace();
                }
        }
        
        /**
         * 
         * @param model   反射对象
         * @param name    反射域
         * @param method set/get
         * @param value    重新赋值
         * @return
         */
        public static Object method(Object model,String name,String method,Object value) {
            try {
                    Method m = null;
                    Field field =  model.getClass().getDeclaredField(name);
                    // 获得属性的类型
                    String type = field.getGenericType().toString();
                    // 可以访问私有属性
                    field.setAccessible(true);
                    name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
                    // 如果是类类型,前面包含class,后面跟类名
                    if (type.equals("class java.lang.String")) {
                        if ("get".equals(method)) {
                            m = model.getClass().getMethod(method + name);
                            // get方法获取属性
                            System.out.println(m.invoke(model)); 
                            return (String)m.invoke(model);
                        }else if ("set".equals(method)) {
                            m = model.getClass().getMethod(method + name,String.class);
                            // set方法获取属性
                            m.invoke(model,(String)value);
                        }
                    }
                    if (type.equals("class java.lang.Float")) {
                        if ("get".equals(method)) {
                            m = model.getClass().getMethod(method + name);
                            // get方法获取属性
                            return (Float)m.invoke(model);
                        }else if ("set".equals(method)) {
                            m = model.getClass().getMethod(method + name,String.class);
                            // set方法获取属性
                            m.invoke(model,(Float)value);
                        }
                    }
                    if (type.equals("class java.lang.Date")) {
                        if ("get".equals(method)) {
                            m = model.getClass().getMethod(method + name);
                            // get方法获取属性
                            return (Date)m.invoke(model);
                        }else if ("set".equals(method)) {
                            m = model.getClass().getMethod(method + name,String.class);
                            // set方法获取属性
                            m.invoke(model,(Date)value);
                        }
                    }
                    if (type.equals("class java.lang.Timestamp")) {
                        if ("get".equals(method)) {
                            m = model.getClass().getMethod(method + name);
                            // get方法获取属性
                            return (Timestamp)m.invoke(model);
                        }else if ("set".equals(method)) {
                            m = model.getClass().getMethod(method + name,String.class);
                            // set方法获取属性
                            m.invoke(model,(Timestamp)value);
                        }
                    }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "";
        }
    
    }
    package other;
    
    public class Stu {
    
        
        private String  name;
        
        private String fname;
    
        private Integer age;
    
        private Double  height;
    
        public String getFname() {
            return fname;
        }
    
        public void setFname(String fname) {
            this.fname = fname;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public Double getHeight() {
            return height;
        }
    
        public void setHeight(Double height) {
            this.height = height;
        }
        
    }
  • 相关阅读:
    PHP学习(字符串和变量)
    一个操作配置文件(Reg,ini,XML)的类
    Indy中判断邮件来源
    PHP学习(MSSQL数据库连接)
    辛辛苦苦,写了个INNO的安装脚本
    php连接sqlserver
    Sql Server 使用CTE实现递归查询
    使用序列化和反序列化机制深度复制对象
    ASP.NET中不常用的另类绑定方法<%$ %>
    google map事件监听
  • 原文地址:https://www.cnblogs.com/lxh520/p/8388329.html
Copyright © 2020-2023  润新知