日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Reflection in Java

發(fā)布時間:2023/12/16 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Reflection in Java 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • Reflection in Java
    • reflection
    • coding

Reflection in Java

reflection

Reflection是 Java API , 用于在運行時檢查或修改方法、類和接口的行為。

  • Reflection所需的類在java.lang.reflect這個包。
  • Reflection為我們提供了某對象屬于哪個類,以及該類的哪些方法可以被該對象調(diào)用。
  • 通過Reflection,我們可以在運行時調(diào)用方法(invoke methods),而無需考慮它們private等限制。訪問限定符(access specifier)

Reflection is an APIwhich is used to examine or modify the behavior of methods, classes, interfaces at runtime.

  • The required classes for reflection are provided under java.lang.reflect package.
  • Reflection gives us information about the class to which an object belongs and also the methods of that class which can be executed by using the object.
  • Through reflection we can invoke methods at runtime irrespective of the access specifier used with them.

https://www.geeksforgeeks.org/reflection-in-java/

Reflection 可以用來獲取以下信息

  • 類:使用方法getClass(),獲取某個對象屬于哪個類。
  • 構(gòu)造函數(shù) :使用方法 getConstuctor(),獲取對象所屬的類的公開構(gòu)造函數(shù)。
  • 方法 :使用getMethod()獲取對象所屬的類的公開方法。

Reflection can be used to get information about –

  • Class The getClass() method is used to get the name of the class to which an object belongs.
  • Constructors The getConstructors() method is used to get the public constructors of the class to which an object belongs.
  • Methods The getMethods() method is used to get the public methods of the class to which an objects belongs.
  • coding

    主要學(xué)習(xí)

    通過field name來創(chuàng)建對象,這需要調(diào)用getDeclaredField()方法;

    通過設(shè)置setAccessible(truee)來避開field的限制,比如private的變量,類外面的函數(shù)可以訪問。

    // creates object of the desired field by providing // the name of field as argument to the // getDeclaredField method Field field = cls.getDeclaredField("s"); // s is private field name// 通過setAccessible方法來避開field的限制 field.setAccessible(true);// takes object and the new value to be assigned // to the field as arguments field.set(obj, "contents to override field s");

    示例代碼

    // A simple Java program to demonstrate the use of reflection import java.lang.reflect.Method; import java.lang.reflect.Field; import java.lang.reflect.Constructor;class Test1 {public static void main(String args[]) throws Exception{// Creating object whose property is to be checked// 創(chuàng)建對象Test obj = new Test();// Creating class object from the object using// getclass method// 使用getClass方法創(chuàng)建對象Class cls = obj.getClass();System.out.println("The name of class is " +cls.getName());// Getting the constructor of the class through the// object of the class// 使用getConstructor方法來獲取構(gòu)造函數(shù)Constructor constructor = cls.getConstructor();System.out.println("The name of constructor is " +constructor.getName());System.out.println("The public methods of class are : ");// Getting methods of the class through the object// of the class by using getMethodsMethod[] methods = cls.getMethods();// Printing method namesfor (Method method:methods)System.out.println(method.getName());System.out.println();// creates object of desired method by providing the// method name and parameter class as arguments to// the getDeclaredMethodMethod methodcall1 = cls.getDeclaredMethod("method2",int.class);// invokes the method at runtimemethodcall1.invoke(obj, 19);// creates object of the desired field by providing// the name of field as argument to the// getDeclaredField methodField field = cls.getDeclaredField("s"); // private field here// allows the object to access the field irrespective// of the access specifier used with the field// 通過setAccessible方法來避開field的限制field.setAccessible(true);// takes object and the new value to be assigned// to the field as argumentsfield.set(obj, "JAVA");// Creates object of desired method by providing the// method name as argument to the getDeclaredMethodMethod methodcall2 = cls.getDeclaredMethod("method1");// invokes the method at runtimemethodcall2.invoke(obj);// Creates object of the desired method by providing// the name of method as argument to the// getDeclaredMethod methodMethod methodcall3 = cls.getDeclaredMethod("method3");// allows the object to access the method irrespective// of the access specifier used with the methodmethodcall3.setAccessible(true);// invokes the method at runtimemethodcall3.invoke(obj);} }// class whose object is to be created class Test {// creating a private field 私有成員函數(shù)private String s;// creating a public constructor 構(gòu)造函數(shù)public Test() { s = "Reflection in Java"; }// Creating a public method with no argumentspublic void method1() {System.out.println("The string is " + s);}// Creating a public method with int as argumentpublic void method2(int n) {System.out.println("The number is " + n);}// creating a private methodprivate void method3() {System.out.println("Private method invoked");} }

    測試結(jié)果

    $ java Test1.java Note: Test1.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. The name of class is Test The name of constructor is Test The public methods of class are : method2 method1 wait wait wait equals toString hashCode getClass notify notifyAllThe number is 19 The string is JAVA Private method invoked

    summary: In Java, we can use the features of reflection to modify private fields. 今天學(xué)習(xí)了Java 的reflection。

    參考:https://www.geeksforgeeks.org/reflection-in-java/

    總結(jié)

    以上是生活随笔為你收集整理的Reflection in Java的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。