1. 程式人生 > >java中迴圈遍歷實體類的屬性和資料型別以及屬性值

java中迴圈遍歷實體類的屬性和資料型別以及屬性值

package com.walkerjava.test;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Date;
 
 /***
  * 遍歷實體類的屬性和資料型別以及屬性值
  * 
  * @author LiBaozhen
  * @date 2013-1-4 上午10:25:02
  * @company 
  * @version v1.3
  * @see 相關類
  * @since 相關/版本
  */
 public class ReflectTest {
         public static void reflectTest(Object model) throws NoSuchMethodException,
                         IllegalAccessException, IllegalArgumentException,
                         InvocationTargetException {
                 // 獲取實體類的所有屬性,返回Field陣列
                 Field[] field = model.getClass().getDeclaredFields();
                 // 遍歷所有屬性
                 for (int j = 0; j < field.length; j++) {
                         // 獲取屬性的名字
                         String name = field[j].getName();
                         // 將屬性的首字元大寫,方便構造get,set方法
                         name = name.substring(0, 1).toUpperCase() + name.substring(1);
                         // 獲取屬性的型別
                         String type = field[j].getGenericType().toString();
                         // 如果type是類型別,則前面包含"class ",後面跟類名
                         System.out.println("屬性為:" + name);
                         if (type.equals("class java.lang.String")) {
                                 Method m = model.getClass().getMethod("get" + name);
                                 // 呼叫getter方法獲取屬性值
                                 String value = (String) m.invoke(model);
                                 System.out.println("資料型別為:String");
                                 if (value != null) {
                                         System.out.println("屬性值為:" + value);
                                 } else {
                                         System.out.println("屬性值為:空");
                                 }
                         }
                         if (type.equals("class java.lang.Integer")) {
                                 Method m = model.getClass().getMethod("get" + name);
                                 Integer value = (Integer) m.invoke(model);
                                 System.out.println("資料型別為:Integer");
                                 if (value != null) {
                                         System.out.println("屬性值為:" + value);
                                 } else {
                                         System.out.println("屬性值為:空");
                                 }
                         }
                         if (type.equals("class java.lang.Short")) {
                                 Method m = model.getClass().getMethod("get" + name);
                                 Short value = (Short) m.invoke(model);
                                 System.out.println("資料型別為:Short");
                                 if (value != null) {
                                         System.out.println("屬性值為:" + value);
                                 } else {
                                         System.out.println("屬性值為:空");
                                 }
                         }
                         if (type.equals("class java.lang.Double")) {
                                 Method m = model.getClass().getMethod("get" + name);
                                 Double value = (Double) m.invoke(model);
                                 System.out.println("資料型別為:Double");
                                 if (value != null) {
                                         System.out.println("屬性值為:" + value);
                                 } else {
                                         System.out.println("屬性值為:空");
                                 }
                         }
                         if (type.equals("class java.lang.Boolean")) {
                                 Method m = model.getClass().getMethod("get" + name);
                                 Boolean value = (Boolean) m.invoke(model);
                                 System.out.println("資料型別為:Boolean");
                                 if (value != null) {
                                         System.out.println("屬性值為:" + value);
                                 } else {
                                         System.out.println("屬性值為:空");
                                 }
                         }
                         if (type.equals("class java.util.Date")) {
                                 Method m = model.getClass().getMethod("get" + name);
                                 Date value = (Date) m.invoke(model);
                                 System.out.println("資料型別為:Date");
                                 if (value != null) {
                                         System.out.println("屬性值為:" + value);
                                 } else {
                                         System.out.println("屬性值為:空");
                                 }
                         }
                 }
         }
 }

  

http://blog.csdn.net/dongzhouzhou/article/details/8659836 

  1. package com.walkerjava.test;
  2.    
  3.   import java.lang.reflect.Field;
  4.   import java.lang.reflect.InvocationTargetException;
  5.   import java.lang.reflect.Method;
  6.   import java.util.Date;
  7.    
  8.   /***
  9.   * 遍歷實體類的屬性和資料型別以及屬性值
  10.   *
  11.   * @author LiBaozhen
  12.   * @date 2013-1-4 上午10:25:02
  13.   * @company
  14.   * @version v1.3
  15.   * @see 相關類
  16.   * @since 相關/版本
  17.   */
  18.   public class ReflectTest {
  19.   public static void reflectTest(Object model) throws NoSuchMethodException,
  20.   IllegalAccessException, IllegalArgumentException,
  21.   InvocationTargetException {
  22.   // 獲取實體類的所有屬性,返回Field陣列
  23.   Field[] field = model.getClass().getDeclaredFields();
  24.   // 遍歷所有屬性
  25.   for ( int j = 0; j < field.length; j++) {
  26.   // 獲取屬性的名字
  27.   String name = field[j].getName();
  28.   // 將屬性的首字元大寫,方便構造get,set方法
  29.   name = name.substring( 0, 1).toUpperCase() + name.substring( 1);
  30.   // 獲取屬性的型別
  31.   String type = field[j].getGenericType().toString();
  32.   // 如果type是類型別,則前面包含"class ",後面跟類名
  33.   System.out.println( "屬性為:" + name);
  34.   if (type.equals( "class java.lang.String")) {
  35.   Method m = model.getClass().getMethod( "get" + name);
  36.   // 呼叫getter方法獲取屬性值
  37.   String value = (String) m.invoke(model);
  38.   System.out.println( "資料型別為:String");
  39.   if (value != null) {
  40.   System.out.println( "屬性值為:" + value);
  41.   } else {
  42.   System.out.println( "屬性值為:空");
  43.   }
  44. 相關推薦

    java迴圈實體屬性資料型別以及屬性

    package com.walkerjava.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;

    java實體屬性資料型別以及屬性

    和同學接了個外包的活,由於專案中很多地方要用到poi匯出excel,而每次匯出都要寫很多相同的程式碼,因為poi的cell.setCellValue();每次設定的都是不同實體bean的屬性值,導致程式碼裡很多重複的值,我在想有沒有可以自動裝載bean的屬性及屬性值的方法。

    java迴圈刪除ListSet集合元素的方

    1.異常java一邊遍歷一邊刪除集合中的元素會報異常ConcurrentModificationException 2.正確的做法如下:   package list; import java.util.*; public class Demo { public

    java實體屬性型別屬性

    [java] view plain copy print? publicstaticvoid testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentExcept

    java實體屬性型別,並賦獲取

    import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Dat

    java實體屬性和數據以及屬性

    取值 .get 數組 所有 system blog ++ 實體bean lds 遍歷實體類的樹形和數據類型一級屬性值 /** * 遍歷實體類的屬性和數據類型以及屬性值 * @param model * @throws NoSuchMe

    Java實體(處理MongoDB)

    boot common ech set declare code mod exc reflect 在實際過程中,經常要將實體類進行封裝,尤其是處理數據庫的過程中;因此,對於遍歷實體類能夠與數據庫中的一行數據對應起來。 我是使用的環境是Spring boot,訪問的數據庫時M

    JAVA-實體屬性

    package com.cpic.caf.template.home.util; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; import o

    實體屬性

    main int lds true () cep declared person generate public static void main(String[] args) throws IllegalArgumentException, IllegalAccessEx

    Java通過例項得到實體屬性屬性

    方式一(實體類):1234567//java中遍歷實體類,獲取屬性名和屬性值public static void testReflect(Object model) throws Exception{for (Field field : model.getClass().ge

    java為什麽實體需要實現序列化

    color 客戶端訪問 tro 我們 str 操作 bsp div zab 當客戶端訪問某個能開啟會話功能的資源,web服務器就會創建一個HTTPSession對象,每個HTTPSession對象都會占用一定的內存,如果在同一個時間段內訪問的用戶太多,就會消耗大量的服務器

    Java如何Map對象的4種方法

    highlight lai put iter popu out tro blog 二叉 https://blog.csdn.net/tjcyjd/article/details/11111401 Java 中Map 根據底層數據結構的不同,存在多種不同的實現,常見如散列

    Java什麽是實體實體的作用

    Java實體類實體類是在JAVA軟件開發中廣泛使用的概念。 但是網上少有講清楚到底是什麽的。本期我來講一講什麽是實體類。 首先,直觀的看:實體類就是一個擁有Set和Get方法的類。實體類通常總是和數據庫之類的(所謂持久層數據)聯系在一起。這種聯系是借由框架(如Hibernate)來建立的。其次說定義(比較生澀

    WPF TreeView 迴圈查詢要素並快速定位

    /// <summary> /// 獲取當前節點下符合條件的子元素 /// </summary> /// <param name="container">當前節點</param> ///

    Java如何Map物件的4種方法

    注:本文參考了浮雲中的神馬中的部落格內容。 在自己程式設計的時候遇到Map的遍歷問題,所以參考網上的部落格總結了一下。以下方法適用於任何map實現(HashMap,TreeMap,LinkedHashMap,Hashtable等)。 方法一:在for-eac

    Java Map

    關於java中遍歷map具體哪四種方式,請看下文詳解吧。 方式一 這是最常見的並且在大多數情況下也是最可取的遍歷方式。在鍵值都需要時使用。 1 2 3 4 Map<Integer, Integer> map = new HashM

    JavaList的幾個問題

    之前在專案中引入Lambda表示式後,最近就把之前的程式碼改為Lambda表示式,中間遇到了一個小插曲就是List的在呼叫Stream的forEach()中使用return 、break、continue關鍵字的問題;加上最近一直關注的“碼農每一題”於是自己回顧

    JavaHashMap的兩種方式

    第一種:   Map map = new HashMap();   Iterator iter = map.entrySet().iterator();   while (iter.hasNext())

    Jquery 迴圈 選擇器 each()

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="https://cdn.stat

    java 列舉 迴圈以及一些簡單常見的使用

    什麼時候想用列舉型別: 有時候,在設計一個java model物件的時候,你需要一些具體的常量字串之類的東西,這個東西又沒必要跟整個專案的全域性常量放在一起,就放在model的java檔案裡面是最合適的,那麼,你可以有兩種選擇: 1,在java model檔案裡面,定義pu