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); } }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值