##What is reflection?
Since reflection has "reverse", it means that it is a reverse process. So let’s first take a look at what the forward process is like. Design a class, instantiate the class into an object, and the object calls methods or properties to implement some functions. What is reflection? I don’t know if you still remember the picture above. This is the knowledge I learned when I first came into contact with java. My own understanding is that now I know 1, infer to 2, and then get some other information through 2. (This is just my speculation. As for whether it is true or not, we need to delve into it in the future) The analogy to the real-life scenario is:你:妈妈,你知道剪刀在哪吗? 妈妈:我那天放针线的时候好像在针线盒里看到了,你去找找有没有。针线盒在客厅的茶几上。 你:好的。
Find the sewing box - get the Class class
First of all, what is the Class class?
The Class class is the class of the People class, just like the People class is the class of p. The Class class is the "grandfather" of p. We can understand it this way: the instance object of java.lang.Class is People, and the instance object of People is p. Our idea is to obtain the Class class through the People class or p object, and then obtain the People class information, such as attributes, methods, constructors, etc., through the Class class. Now that we know what the Class class is, let’s talk about how to get it. There are three ways to find the Class class: It is worth mentioning that c1, c2, and c3 are called the "class types" of the People class. Okay, now let’s summarize the known conditions: People: Classp: Objectc: Class TypeFind the scissors - get the information of the People class
Application 1: Get all the methods of the class
In the method of obtaining the People class, ms is an array that stores the People class method. Through a loop, its name, parameter type, and return value are obtained layer by layer. Finally, just call it directly in the main function The partial results obtained are as follows:
Application 2: Get all the attributes of the class
##The call is similar to the calling method:
Run result:
Application 3: Obtain the constructor of the classThe constructor is similar, but not Again.
Application 4: Obtain a specific method of the class
The running results are as follows. The results of the two calling methods are the same: The above is just a list of individual functions. For more specific information, you can find it in the API documentation: http:/ /tool.oschina.net/apidocs/apidoc?api=jdk-zh
##The concept of reflection What is the definition of reflection? Is reflection necessary? Disadvantages of reflection? What are the criteria for whether to use reflection? Java introductory tutorials, welcome to learn online!
The above is the detailed content of what is java reflection. For more information, please follow other related articles on the PHP Chinese website!