java中遍历属性字段及值(常见方法)

package com.js.s;

import java.lang.reflect.InvocationTargetException;

public class meiju {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
c model = new c();
meiju output = new meiju();
output.AllByType(model);
output.AllByObject(model);
output.allByGet(model);
}

//根据类型来遍历对象的属性
public void AllByType(c model) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
    System.out.println("根据类型来遍历对象的属性");
    java.lang.reflect.Field[] field = model.getClass().getDeclaredFields();        //获取实体类的所有属性,返回Field数组  
    for(int j=0 ; j<field.length ; j++){     //遍历所有属性
            String name = field[j].getName();    //获取属性的名字
            
            System.out.println("attribute name:"+name);     
            name = name.substring(0,1).toUpperCase()+name.substring(1); //将属性的首字符大写,方便构造get,set方法
            String type = field[j].getGenericType().toString();    //获取属性的类型
            if(type.equals("class java.lang.String")){   //如果type是类类型,则前面包含"class ",后面跟类名
                java.lang.reflect.Method m = model.getClass().getMethod("get"+name);
                String value = (String) m.invoke(model);    //调用getter方法获取属性值
                if(value != null){
                    System.out.println("attribute value:"+value);
                }
            }
            if(type.equals("int")){     
                java.lang.reflect.Method m = model.getClass().getMethod("get"+name);
                Integer value = (Integer) m.invoke(model);
                if(value != null){
                    System.out.println("attribute value:"+value);
                }
            }
    }
}

//将所有的属性转换对象
public void AllByObject(c model) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
    System.out.println("将所有的属性转换对象");
    java.lang.reflect.Field[] field = model.getClass().getDeclaredFields();        //获取实体类的所有属性,返回Field数组  
    for(int j=0 ; j<field.length ; j++){     //遍历所有属性
        String name = field[j].getName();    //获取属性的名字
        System.out.println("attribute name:"+name);     
        name = name.substring(0,1).toUpperCase()+name.substring(1); //将属性的首字符大写,方便构造get,set方法
        java.lang.reflect.Method m = model.getClass().getMethod("get"+name);
        Object value = (Object) m.invoke(model);    //调用getter方法获取属性值
        if(value != null){
            System.out.println("attribute value:"+value.toString());
        }
    }
}

//get方法
public void allByGet(c model) throws IllegalArgumentException, IllegalAccessException{
    System.out.println("get方法");
    java.lang.reflect.Field[] flds = model.getClass().getFields();  
    if ( flds != null )  
    {  
        for ( int i = 0; i < flds.length; i++ )  
        {  
            System.out.println(flds[i].getName() + " - " + flds[i].get(model));  
        }  
    }  
}

}

class c{
public String k = "10";
public int age = 20;

public String getK() {
    return k;
}
public void setK(String k) {
    this.k = k;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

}

转载于:https://www.cnblogs.com/TanMG/p/5546625.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值