java遍歷實體類的屬性和值


代碼如下:

實體類:

public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private String pkid;
    
    private String userName;
    
    private String passWord;
    
    private String roleID;
}

遍歷:

public class test {

        public static void main(String[] args) throws Exception{  
            User e = new User();  
            reflect(e);  
        }
        
        public static void reflect(User e) throws Exception{  
            Class cls = e.getClass();  
            Field[] fields = cls.getDeclaredFields();  
            for(int i=0; i<fields.length; i++){  
                Field f = fields[i];  
                f.setAccessible(true);  
                System.out.println("屬性名:" + f.getName() + " 屬性值:" + f.get(e));  
            }   
        } 
}

輸出:

 


免責聲明!

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



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