Reflection in Java
文章目錄
- Reflection in Java
- reflection
- coding
Reflection in Java
reflection
Reflection是 Java API , 用于在運(yùn)行時檢查或修改方法、類和接口的行為。
- Reflection所需的類在java.lang.reflect這個包。
- Reflection為我們提供了某對象屬于哪個類,以及該類的哪些方法可以被該對象調(diào)用。
- 通過Reflection,我們可以在運(yùn)行時調(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 –
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 invokedsummary: 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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机密码学论文,密码学论文写作范例论文
- 下一篇: 工厂模式及在Spring中的应用