Java 通過反射獲取類的資訊(成員變數,成員方法,構造方法)

言曌發表於2018-03-03

直接看程式碼

  1. package practive.Reflect;
  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5. /**
  6.  * 獲得類的資訊(成員方法,成員變數,構造方法)
  7.  * @Author: 言曌
  8.  * @Date: 2017/11/16
  9.  * @Time: 下午10:06
  10.  */
  11. public class ClassUtil {
  12.     public static void printMrthodMessage(Object obj) {
  13.         //獲取類的資訊,首先要獲取類的類型別
  14.         Class c = obj.getClass();//傳遞是哪個子類的物件,c 就是該子類的類型別
  15.         //獲取類的名稱
  16.         System.out.println("類的名稱:"+c.getName());
  17.         /**
  18.          * Method類,方法物件
  19.          * 一個成員方法是一個Method物件
  20.          * getMethods()方法獲取的是所有public的函式,包括繼承而來的
  21.          * getDeclaredMethods()獲取的是所有該類的宣告的方法,不問訪問許可權
  22.          */
  23.         Method[] ms = c.getMethods();
  24.         for(int i=0;i<ms.length;i++) {
  25.             //獲得方法的返回值型別的類型別
  26.             Class returnType = ms[i].getReturnType();
  27.             System.out.print(returnType+" ");
  28.             //得到方法名
  29.             System.out.print(ms[i].getName()+"(");
  30.             //獲得引數型別-->得到的是引數列表的型別的類型別
  31.             Class[] paramTypes = ms[i].getParameterTypes();
  32.             for(Class class1 : paramTypes) {
  33.                 System.out.print(class1.getName()+",");
  34.             }
  35.             System.out.print(")");
  36.             System.out.println();
  37.         }
  38.     }
  39.     public static void printFieldMessage(Object obj) {
  40.         Class c = obj.getClass();
  41.         /**
  42.          * 成員變數也是物件
  43.          * java.lang.reflect.Field
  44.          * Field類封裝了關於成員變數的操作
  45.          * getFields()方法獲取的是所有的public的成員變數的資訊
  46.          * getDeclaredFields()獲取的是該類自己宣告的成員變數的資訊
  47.          */
  48.         //Field[] fs = c.getFields();
  49.         Field[] fs = c.getDeclaredFields();
  50.         for(Field field : fs) {
  51.             //得到成員變數的型別的類型別
  52.             Class fieldType = field.getType();
  53.             String typeName = fieldType.getTypeName();
  54.             //獲得成員變數的名稱
  55.             String fieldName = field.getName();
  56.             System.out.println(typeName+" "+fieldName);
  57.         }
  58.     }
  59.     public static void printConMessage(Object obj) {
  60.         Class c = obj.getClass();
  61.         /**
  62.          * 建構函式也是物件
  63.          * java.lang.Constructor中封裝了建構函式的資訊
  64.          * getConstructors獲取所有的public的建構函式
  65.          * getDeclaredConstuctors()得到所有的建構函式
  66.          */
  67.         //Constructor[] cs = c.getConstructors();
  68.         Constructor[] cs = c.getDeclaredConstructors();
  69.         for(Constructor constructor:cs) {
  70.             System.out.print(constructor.getName()+"(");
  71.             //獲取建構函式的引數列表-->得到的是引數列表的類型別
  72.             Class[] paramTypes = constructor.getParameterTypes();
  73.             for(Class class1:paramTypes) {
  74.                 System.out.print(class1.getName()+",");
  75.             }
  76.             System.out.print(")");
  77.             System.out.println();
  78.         }
  79.     }
  80. }

 

然後寫一個類來測試

  1. package practive.Reflect;
  2. /**
  3.  * @Author: 言曌
  4.  * @Date: 2017/11/16
  5.  * @Time: 下午10:30
  6.  */
  7. public class ClassDemo2 {
  8.     public static void main(String args[]) {
  9.         ClassUtil.printMethodMessage("hello");
  10.         ClassUtil.printMethodMessage(new Integer(1));
  11.         //ClassUtil.printFieldMessage("hello");
  12.         ClassUtil.printFieldMessage(new Integer(1));
  13.         //ClassUtil.printConMessage("hello");
  14.         ClassUtil.printConMessage(new Integer(1));
  15.     }
  16. }

 


相關文章