把Core Java I Reflection那一節(jié)打印下來準備慢慢看支持泛型后的反射機制的用法,卻看到
          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
          用于動態(tài)創(chuàng)建對象數組。
          示例代碼用于擴大一個任意類型的數組
          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)
          第一個參數必須有,如果是靜態(tài)方法,該參數為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 閱讀(298) 評論(0)  編輯  收藏 所屬分類: OOP
          2007-02-27 13:51:48
          只有注冊用戶登錄后才能發(fā)表評論。


          網站導航:
           
          主站蜘蛛池模板: 延吉市| 兖州市| 涿鹿县| 大方县| 和平县| 临江市| 通城县| 文化| 扶沟县| 平安县| 屯留县| 大荔县| 福海县| 浑源县| 太保市| 三都| 当雄县| 临桂县| 来凤县| 桐乡市| 荣成市| 保德县| 安塞县| 垦利县| 肥乡县| 吴堡县| 施甸县| 太仓市| 定日县| 黄浦区| 体育| 社旗县| 岫岩| 都昌县| 泸水县| 阿尔山市| 安义县| 平南县| 甘洛县| 呼图壁县| 喀喇沁旗|