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

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM