zoukankan      html  css  js  c++  java
  • Java循环一个对象的所有属性,并通过反射给这些属性赋值/取值

    Java循环一个对象的所有属性,并通过反射给这些属性赋值/取值

    说到循环遍历,最常见的遍历数组/列表、Map等。但是,在开发过程中,有时需要循环遍历一个对象的所有属性。遍历对象的属性该如何遍历呢?查了一下资料,需要用到一些反射的知识!

    话不多说,先上代码

    首先先定义一个测试对象 Test

    public class Test {
    
        private String aa;
    
        private int bb;
    
        private String cc;
    
        public String dd;
    
        public String getAa() {
            return aa;
        }
    
        public void setAa(String aa) {
            this.aa = aa;
        }
    
        public int getBb() {
            return bb;
        }
    
        public void setBb(int bb) {
            this.bb = bb;
        }
    
        public String getCc() {
            return cc;
        }
    
        public void setCc(String cc) {
            this.cc = cc;
        }
    }

    这个对象里分别有aa bb cc 三个私有属性,dd一个公有属性,接下来通过反射来获取这个对象的四个属性值

        public static void main(String[] args) {
    
            /**
             *  返回Class 对象所表示的类或接口的所有可访问公共字段。
             */
            Field[] f1=Test.class.getFields();
            System.out.println("Test类里面的公共字段属性的个数为:" +f1.length+"个,分别为:");
            for(int i=0;i<f1.length;i++){
                String attributeName=f1[i].getName();
                System.out.println(attributeName);
            }
    
    
            /**
             * 返回 Class 对象所表示的类或接口所声明的所有字段,
             * 包括公共、保护、默认(包)访问和私有字段,但不包括继承的字段。
             */
            Field[] f=Test.class.getDeclaredFields();
            System.out.println("Test类里面的所有字段属性的个数为:"+f.length+"个,分别为:");
            for(int i=0;i<f.length;i++){
                String attributeName=f[i].getName();
                System.out.println(attributeName);
            }
    
        }

    运行main函数,输出如下:

    Test类里面的公共字段属性的个数为:1个,分别为: 
    dd 
    Test类里面的所有字段属性的个数为:4个,分别为: 
    aa 
    bb 
    cc 
    dd

    getFields()方法只能获取Test类里面的所有公有属性,getDeclaredFields()方法可获取Test类所有公有/私有属性。需要注意的是,如果Test类继承了别的类,这两个方法都无法获取父类里面的属性的。

    现在,已经获取到了Test类里面的所有属性名,然后通过这些属性名给这个类的实例赋值和取值操作。

    先来给实例赋值,代码如下:

            Test test=new Test();
            //给test对象赋值
            for(int i=0;i<f.length;i++){
                //获取属相名
                String attributeName=f[i].getName();
                //将属性名的首字母变为大写,为执行set/get方法做准备
                String methodName=attributeName.substring(0,1).toUpperCase()+attributeName.substring(1);
                try{
                    //获取Test类当前属性的setXXX方法(私有和公有方法)
                    /*Method setMethod=Test.class.getDeclaredMethod("set"+methodName);*/
                    //获取Test类当前属性的setXXX方法(只能获取公有方法)
                    Method setMethod=Test.class.getMethod("set"+methodName,String.class);
                    //执行该set方法
                    setMethod.invoke(test,attributeName+"方法赋值");
                }catch (NoSuchMethodException e) {
                    try {
                        Method setMethod=Test.class.getMethod("set"+methodName,int.class);
                        setMethod.invoke(test,123);
                    } catch (Exception e2) {
                        f[i].set(test,attributeName+"直接赋值");
                    }
    
                }
    
            }   

    如上代码,之前已经获取到了Test类里面的所有属性名,在这部分代码中,就可以通过属性名来获取与之对应的set方法。需要注意的是,在上面代码中try-catch了两次,是因为在Test这个类中的属性的set方法的参数,有的是String类型,有的是int类型的,还有的属性是没有set方法的。第一次try是未了抓取所有参数类型不是String类型和不存在的set方法。第二次抓取的是参数不是int类型的和不存在的set方法。依次进行处理。除此之外,假如set方法的参数类型还有更多的类型的话,可以通过Filed类的getGenericType() 这个方法先判断类型,这个以后再做赘述。

    接下来从实例中通过get方法来取值,代码如下:

            //从test对象取值
            for(int i=0;i<f.length;i++){
                //获取属相名
                String attributeName=f[i].getName();
                //将属性名的首字母变为大写,为执行set/get方法做准备
                String methodName=attributeName.substring(0,1).toUpperCase()+attributeName.substring(1);
                Object result;
                try{
                    //获取Test类当前属性的setXXX方法(私有和公有方法)
                    /*Method setMethod=Test.class.getDeclaredMethod("set"+methodName);*/
                    //获取Test类当前属性的setXXX方法(只能获取公有方法)
                    Method getMethod=Test.class.getMethod("get"+methodName);
                    //执行该set方法
                    result=getMethod.invoke(test);
    
                }catch(NoSuchMethodException e){
                    result=f[i].get(test);
                }
                System.out.println("属性:"+attributeName+"="+result);
            }

    这里的代码也try-catch一次,get方法不存在参数类型判断的,这里的try-catch只是为了抓取公有属性不存在get方法的情况以做相应的处理。

    运行测试程序,输出结果如下:

    属性:aa=aa方法赋值 
    属性:bb=123 
    属性:cc=cc方法赋值 
    属性:dd=dd直接赋值

    最后附上完整的测试程序

        public static void main(String[] args) throws Exception{
    
            /**
             *  返回Class 对象所表示的类或接口的所有可访问公共字段。
             */
            Field[] f1=Test.class.getFields();
            System.out.println("Test类里面的公共字段属性的个数为:" +f1.length+"个,分别为:");
            for(int i=0;i<f1.length;i++){
                String attributeName=f1[i].getName();
                System.out.println(attributeName);
            }
    
            /**
             * 返回 Class 对象所表示的类或接口所声明的所有字段,
             * 包括公共、保护、默认(包)访问和私有字段,但不包括继承的字段。
             */
            Field[] f=Test.class.getDeclaredFields();
            System.out.println("Test类里面的所有字段属性的个数为:"+f.length+"个,分别为:");
            for(int i=0;i<f.length;i++){
                String attributeName=f[i].getName();
                System.out.println(attributeName);
            }
    
            Test test=new Test();
            //给test对象赋值
            for(int i=0;i<f.length;i++){
                //获取属相名
                String attributeName=f[i].getName();
                //将属性名的首字母变为大写,为执行set/get方法做准备
                String methodName=attributeName.substring(0,1).toUpperCase()+attributeName.substring(1);
                try{
                    //获取Test类当前属性的setXXX方法(私有和公有方法)
                    /*Method setMethod=Test.class.getDeclaredMethod("set"+methodName);*/
                    //获取Test类当前属性的setXXX方法(只能获取公有方法)
                    Method setMethod=Test.class.getMethod("set"+methodName,String.class);
                    //执行该set方法
                    setMethod.invoke(test,attributeName+"方法赋值");
                }catch (NoSuchMethodException e) {
                    try {
                        Method setMethod=Test.class.getMethod("set"+methodName,int.class);
                        setMethod.invoke(test,123);
                    } catch (Exception e2) {
                        f[i].set(test,attributeName+"直接赋值");
                    }
    
                }
    
            }
            //从test对象取值
            for(int i=0;i<f.length;i++){
                //获取属相名
                String attributeName=f[i].getName();
                //将属性名的首字母变为大写,为执行set/get方法做准备
                String methodName=attributeName.substring(0,1).toUpperCase()+attributeName.substring(1);
                Object result;
                try{
                    //获取Test类当前属性的setXXX方法(私有和公有方法)
                    /*Method setMethod=Test.class.getDeclaredMethod("set"+methodName);*/
                    //获取Test类当前属性的setXXX方法(只能获取公有方法)
                    Method getMethod=Test.class.getMethod("get"+methodName);
                    //执行该set方法
                    result=getMethod.invoke(test);
    
                }catch(NoSuchMethodException e){
                    result=f[i].get(test);
                }
                System.out.println("属性:"+attributeName+"="+result);
            }
    
        }
  • 相关阅读:
    php 编码规范(1)
    ubuntu 引导删除
    http协议详解
    centos 下mysql操作
    php 异常捕获
    php 设置报错等级
    在WINDOWS SERVER 上或远程桌面中使用 MUTEX
    发送WIN+SAPCE键,WINDOWS,空格键
    访问本机的WEB API 报400错误
    php.ini xdebug
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/9705206.html
Copyright © 2011-2022 走看看