反射小結

 1. Class: 是一個類; 一個描述類的類.

  封裝了描述方法的 Method,

       描述字段的 Filed,

              描述構造器的 Constructor 等屬性.
 
 2. 如何得到 Class 對象:
    2.1 Person.class
    2.2 person.getClass()
    2.3 Class.forName("com.atguigu.javase.Person")
  
 3. 關於 Method:
    3.1 如何獲取 Method:
      1). getDeclaredMethods: 得到 Method 的數組.
      2). getDeclaredMethod(String methondName, Class ... parameterTypes)
  
    3.2 如何調用 Method
      1). 如果方法時 private 修飾的, 需要先調用 Method 的 setAccessible(true), 使其變爲可訪問
      2). method.invoke(obj, Object ... args);
  
  4. 關於 Field:
    4.1 如何獲取 Field: getField(String fieldName)
    4.2 如何獲取 Field 的值: 
      1). setAccessible(true)
      2). field.get(Object obj)
    4.3 如何設置 Field 的值:
      field.set(Obejct obj, Object val)
  
  5. 瞭解 Constructor 和 Annotation 
  
  6. 反射和泛型.
    6.1 getGenericSuperClass: 獲取帶泛型參數的父類, 返回值爲: BaseDao<Employee, String>
    6.2 Type 的子接口: ParameterizedType
    6.3 可以調用 ParameterizedType 的 Type[] getActualTypeArguments() 獲取泛型參數的數組.

相關文檔:https://www.cnblogs.com/tech-bird/p/3525336.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章