把Core Java I Reflection那一節打印下來準備慢慢看支持泛型后的反射機制的用法,卻看到
          We are not dwelling on this issue because it would further complicate an already abstract concept. For most practical purposes, you can ignore the type parameter and work with the raw Class type.
          -,-
          好吧,既然都打印出來,還是看完了再說
          1. Class.getMethods()方法返回一個Method數組,包括了所有類自身及繼承下來的public方法
          類似的有getFields() getConstructors()
          要訪問私有成員,先調用setAccessible(true),這是繼承自AccessibleObject類的一個方法。
          2. java.lang.reflect.Array
          用于動態創建對象數組。
          示例代碼用于擴大一個任意類型的數組
          static Object goodArrayGrow(Object a) {
          Class cl = a.getClass();
          if (!cl.isArray()) return null;
          Class componentType = cl.getComponentType();
          int length = Array.getLength(a);
          int newLength = length * 11 / 10 + 10;
          Object newArray = Array.newInstance(componentType, newLength);
          System.arraycopy(a, 0, newArray, 0, length);
          return newArray;
          }
          int[] a = {1, 2, 3, 4};
          a = (int[]) goodArrayGrow(a);
          這樣也是正確的,因為返回值是Object,因此可以轉型為int[]
          3. NB的東東-方法指針
          正如Field類有get方法,Method類也有invoke方法。
          Object invoke(Object obj, Object... args)
          第一個參數必須有,如果是靜態方法,該參數為null。
          假設m1代表Employee類的getName方法,下面的代碼就調用了這個方法
          String n = (String) m1.invoke(harry); //harry is a Employee
          如果返回值是基本類型,Java通過autoboxing返回它們的wrapper類。
          要獲得Method類,可以從getdeclaredMethods方法的返回值中找到,也可以使用getMethod方法
          Mathod getMethod(String name, Class... parameterTypes)
          如要從Employee類中獲得raiseSalary(double)這個方法
          Method m2 = Employee.class.getMethod("raiseSalary", double.class);

          posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          2.27 Java notes - Reflection

          Posted on 2007-04-22 20:25 ZelluX 閱讀(299) 評論(0)  編輯  收藏 所屬分類: OOP
          2007-02-27 13:51:48
          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 仙居县| 常德市| 偏关县| 嘉禾县| 沧州市| 江川县| 宝兴县| 文水县| 民勤县| 五常市| 开江县| 景谷| 镇赉县| 衡阳市| 龙州县| 斗六市| 麻栗坡县| 平遥县| 高碑店市| 正镶白旗| 舟山市| 富蕴县| 平果县| 揭东县| 化州市| 苏尼特左旗| 永嘉县| 荃湾区| 临湘市| 拜泉县| 个旧市| 红原县| 宣化县| 金沙县| 鹿邑县| 中卫市| 澳门| 潼关县| 宜章县| 区。| 乳源|