reflex

1、 What is reflection

Java programs can dynamically load, parse and use some data types that are uncertain in the compilation stage

2、 Reflection function

2.1. Load the data type that can only be determined at runtime 2.2. Analyze the structure of the class and obtain internal information 2.3. Operate the type or its instance: access properties, call methods and create new objects

3、 Method to get class object

3.1. Get the static function forname() of class

3.2. Call the getclass() function inherited from object

3.3. Adoption Class expression get

4、 Reflection operation constructor

4.1 instantiate the object by calling the constructor with no parameters, one parameter and multiple parameters

Output results:

3com. study. test. aaa(int,java.lang.String)com. study. test. aaa(int)com. study. test. aaa()aaa()aaa(int i)aaa(int i,String j)

4.2 get the modifier, name, and parameters of the constructor

Output results:

3 get the modifier, name and parameter type of the constructor 0com study. test. aaaintclass java. Lang.string get modifier, name and parameter type of constructor 0com study. test. Aaaint gets the modifier, name and parameter type of the constructor 0com study. test. aaa

5、 Reflection acquisition interface

Output results:

Get interface com study. test. I1interface com. study. test. I2

6、 Reflection gets the return type, modifier, parameter type, etc. of the method

Output results:

The return type, parameter type, modifier of the output method, and the exception type of the method, void 00void com study. test. aaa. test() throws java. Lang.exception outputs the return type, parameter type, modifier, and exception type of the method. Void 00 outputs the return type, parameter type, modifier, and exception type of the method. Void 00

7、 Calling methods through reflection

Output results:

Get all the methods declared in this class, public void com study. test. aaa. test()public void com. study. test. aaa. test1(int)void com. study. test. aaa. Test2 (int, int) constructor protected com study. test. aaa(int,int)public com. study. test. aaa(int)public com. study. test. The aaa() method calls aaa() void test() AAA (int i) void test1()

8、 Reflection gets all the fields

Output results:

twenty-one

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>