Java reflection – dry goods

After reading many articles on java reflection, I summarized what I saw. Help yourself and others more or less.

First, let's take a look at the description of reflection in Oracle:

Java reflection mechanism is to know all the properties and methods of any class in the running state; For any object, you can call any of its methods and properties; This kind of dynamically acquired information and the function of dynamically calling object methods are called the reflection mechanism of Java language.

In short:

To put it more simply:

for instance:

Java is an object-oriented language. We can classify (Abstract) tigers, lions and other animals with the same properties as cats. They have some properties such as teeth, beard and so on. At the same time, it has the action of eating meat ().

For the same reason, the classes we come into contact with can also be abstracted, including class name, member variables, methods, etc.

Now that you can treat a class as an object, Java can handle it.

There are three ways to get class:

1. Use the forname static method of class

2. Get the class of an object directly

3. Call the getClass () method of an object

The following is a complete demo showing the operation of java reflection:

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
分享
二维码
< <上一篇
下一篇>>