溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java中內(nèi)部類靜態(tài)成員變量的值怎么利用反射機制獲取

發(fā)布時間:2020-12-04 15:11:14 來源:億速云 閱讀:352 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)java中內(nèi)部類靜態(tài)成員變量的值怎么利用反射機制獲取,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

待解析類結(jié)構(gòu)如下:

/**
 * @Author changle
 * @Time 17/6/13.
 * @Desc to do
 */
public class Goods {
 static class apple{
  public static String version = "iphone6s[是手機不是吃的蘋果]";
  public static String date = "生產(chǎn)日期 2017-06-13";
 }
}

獲取內(nèi)部類靜態(tài)成員變量工具類:

/**
 * @Author changle
 * @Time 17/6/13.
 * @Desc 獲取靜態(tài)內(nèi)部類靜態(tài)變量
 */
public class TestParseInnerProValue {
 public static void main(String[] args) {
  Class<&#63;> clasz = Goods.class;
  printInnerParamValue(clasz);
 }
 
 public static void printInnerParamValue(Class<&#63;> clasz){
  Class innerClazz[] = clasz.getDeclaredClasses();
  for(Class claszInner : innerClazz){
   Field[] fields = claszInner.getDeclaredFields();
   for(Field field : fields){
    try {
     Object object = field.get(claszInner);
     System.out.println("獲取到的feild, name=" + field.getName()+", value="+ object.toString());
     //打印內(nèi)容
     /*
     * 獲取到的feild, name=version, value=iphone6s[是手機不是吃的蘋果]
      獲取到的feild, name=date, value=生產(chǎn)日期 2017-06-13
     * */
    } catch (IllegalAccessException e) {
     e.printStackTrace();
    }
 
   }
  }
 }
}

補充知識:Java 利用反射機制獲取內(nèi)部類的私有屬性和方法,并且修改私有屬性的值

廢話不多說直接貼代碼,代碼中有注釋,如下:

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
  @SuppressWarnings("unused")
  class Test1{
    //私有屬性
    private String color = "blue";
    
    //無參數(shù)的私有方法使用私有屬性
    private void queryColor(){
      System.out.println(color);
    }
    
    //一個參數(shù)的私有方法
    private void privateTest1(String param1) {
      System.out.println(">>>>>>" + param1 + ">>>>>>>>>" + color +">>>>>>>>>>");
    }
    
    //兩個參數(shù)的私有方法
    private void privateTest2(String param1,String param2) {
      System.out.println(">>>>>>" + param1 + ">>>>>>>>>"+ param2 + ">>>>>>>>");
    }    
  }
  
  /** 
   * @Title: main 
   * @Description: 利用反射機制獲取內(nèi)部類的私有屬性和方法,并且修改私有屬性的值
   * @param args
   * @author songye 
   * @date Jul 19, 2018 10:17:32 AM
   * @return: void
   */
  public static void main(String[] args) {
    try {
      Test test = new Test(); 
      Test1 test1 = test.new Test1();
      //獲取內(nèi)部類的私有屬性
      Field colorField = Test1.class.getDeclaredField("color");
      //設(shè)置為true表示可以訪問private修飾的私有屬性
      colorField.setAccessible(true);
      System.out.println(colorField);
      //color的值改為red
      colorField.set(test1, "red");
      
      //獲取內(nèi)部類的私有方法,無參數(shù)的
      Method method = Test1.class.getDeclaredMethod("queryColor");
      //設(shè)置為true表示可以訪問private修飾的私有方法
      method.setAccessible(true);
      System.out.println(method);
      //用來執(zhí)行指定對象的方法,無參數(shù)的
      method.invoke(test1);
      
      //獲取內(nèi)部類的私有方法,一個參數(shù)的
      Method method1 = Test1.class.getDeclaredMethod("privateTest1",String.class);
      //設(shè)置為true表示可以訪問private修飾的私有方法
      method1.setAccessible(true);
      System.out.println(method1);
      //用來執(zhí)行指定對象的方法,前面test1是對象,后面的是一個參數(shù)
      method1.invoke(test1,"我調(diào)用了你的私有方法?。?!");
      
      //獲取內(nèi)部類的私有方法,兩個參數(shù)的
      Method method2 = Test1.class.getDeclaredMethod("privateTest2",String.class,String.class);
      //設(shè)置為true表示可以訪問private修飾的私有方法
      method2.setAccessible(true);
      System.out.println(method2);
      //用來執(zhí)行指定對象的方法,前面test1是對象,后面的是兩個參數(shù)
      method2.invoke(test1, "用我的參數(shù)1","用我的參數(shù)2");
    } catch (NoSuchFieldException e) {
      e.printStackTrace();
    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
  }
}

關(guān)于java中內(nèi)部類靜態(tài)成員變量的值怎么利用反射機制獲取就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI