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;
}

}

posted @ 2016-05-31 17:20  TanMG  阅读(2969)  评论(0编辑  收藏  举报