Java reflex in simple terms, harvesting Kwai, bytes, Baidu, Offer tour of the US group

If you want to show the operation, you must first obtain the Class object, because the Class object represents various types. With it, you can get all kinds of Class information. The acquisition method is as follows:

1) Through object getClass()

public static void main(String[] args) {

        

        Car car = new Car();

        

        Class clazz = car.getClass();

    }

Note: this method is not applicable to int, float and other types

2) Pass (type name) class, packaging Type

public static void main(String[] args) {

        Class clazz = Car.class;

        Class cls1 = int.class;

        Class cls2 = String.class;

        Class cls3=Iteger.Type

    }

3) Pass class Forclass (fully qualified name of string class)

1 try {

2     Class clz = Class.forName("com.frank.test.Car");

3 } catch (ClassNotFoundException e) {

4     e.printStackTrace();

5 }

Which method to use depends on the actual situation.

2.3 obtaining information

After having a Class object, you can get the Class members (Methods + attributes), annotations and Class modifiers. As mentioned above, in java, methods are represented by Method Class, attributes are represented by Field Class, annotations are represented by Annotation Class, and modifiers are represented by Modifier Class. There are corresponding methods in Class to get them, as follows:

2.3. 1 get the object of property Field

1 //Get all properties, but not those inherited from the parent class

2 public Field[] getDeclaredFields() throws SecurityException 

3 //Get all the public properties of itself, including those inherited from the parent class.

4 public Field[] getFields() throws SecurityException



5 //Gets the specified property declared in this class. The parameter is the name of the property

6 public Field getDeclaredField(String name) 

7 //Gets the specified public attribute, including the of the parent class. The parameter is the name of the attribute

8 public Field getField(String name) 

2.3. 2 get Method object

//Get the method specified in this class declaration. The first parameter is the name of the method, and the following parameter is the class of method parameter type,

//For example, get the setName (String name) method, getDeclareMethod ("setName", String.Class)

public Method getDeclaredMethod(String name, Class<?>... parameterTypes)

//Get public methods, including those of the parent class

public Method getMethod(String name, Class<?>... parameterTypes)

//Gets all the methods declared in this class

public Method[] getDeclaredMethods()

//Get all public methods, including those of the parent class

public Method[] getMethods()

2.3. 3. Get Constructor object

//Gets the constructor specified in this class

public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)

//Gets the specified public constructor

public Constructor<T> getConstructor(Class<?>... parameterTypes)

//Get all constructors in this class

public Constructor<?>[] getDeclaredConstructors() throws SecurityException 

//Get all public constructor methods in this class

public Constructor<?>[] getConstructors()

The acquisition of construction method is roughly the same as that of ordinary method.

------------------------------------------------------------------

The above methods are all in the Class. Don't be silly and don't know (don't ask me how I know > >), and then call them through the Class object.

Here is just a list of methods for obtaining common Class information and other information. Please refer to API documents, such as annotations, Class objects of classes (it seems a little around...)

2.4 get class member information

The above only obtains the object of the class represented by the member of the class. We also need to use them or obtain the member information (name, modifier, etc.). Because there is an object representing the member, we can use the object to call the instance method.

2.4.1 Field class

The methods of Field class can be divided into two types: one is to obtain the information of the attribute, and the other is to set the value of the attribute.

First:

 1 //Returns the name of the Field represented by this Field object

 2  String  getName() 

 3 //Returns a class object that identifies the declaration type Field object of the Field represented by this. 

 4 Class<?>  getType() 

 5 //Returns the Java language Modifier of the Field represented by the Field object as an integer. Take the integer as the parameter of Modifier's construction method to obtain the Modifier class object represented by the integer

 6 int  getModifiers() 

 7 ----------------------------------------------------------------

 8 

 9 //Gets the value of a static or instance field of type int, or converted to another original type of type int by extension conversion.

10 int getInt(Object obj) 

11 //Get the value of a static or instance field of type long, or get the value of another basic type that can be converted to type long by expanding the conversion. 

12 long getLong(Object obj) 

13 ......A pile is omitted here get**(Object obj)The basic type of method and attribute is get Just what

14 Property is a reference type, then the following method is called

15 //Returns the Field of the represented Field on the specified object. 16 Object get(Object obj)

Second:

 1 //Set as a field value on the object specified by double.  

 2 void setDouble(Object obj, double d) 

 3 //Set the object specified as a field value float.  

 4 void setFloat(Object obj, float f) 

 5 //Set on the object specified as the value int of a field.

 6 void setInt(Object obj, int i) 

 7 ........A pile is omitted here set**()Method, the basic type of attribute is set Just what

 8 Property is a reference type, then the following method is called

 9 //Sets the Field represented by this Field object on the specified object parameter to the specified new value.

10 void  set(Object obj, Object value) 

Note: if you don't have access permission, you can't set the attribute value by default. What should you do? Can't you show the operation? However, as mentioned earlier, the reflection is very powerful. You can do some unconventional operations,

At this time, we can call the setAccessible (true) method of the Class object!

Do you think the reflection can be strong?



### 2.4.1 Method class



The Method class is mainly used to obtain Method information

Some methods:

1 int getModifiers() //Returns the Java language modifiers of the executable represented by the object.  

2 String getName() //Returns the name of the method represented by this method object as a String.  

3 Annotation[][] getParameterAnnotations() //Returns an array of annotations, which represents the Executable of the declaration order of the formal parameters of the Executable represented by the object.  

4 int getParameterCount() //Returns the number of formal parameters (whether explicitly or implicitly declared) of the executable represented by this object.  

5 Class<?>[] getParameterTypes() //Returns an array of class objects that represent the formal parameter types of the executable represented by the object in declarative order. 

```



### 2.4.1 Constructor class



Constructor The method of class is mainly to obtain the information of constructor method and create object



**Get method information:**



```

1 int getModifiers() //Returns the Java language modifiers of the executable represented by the object.  

2 String getName() //Returns the name of this constructor as a string.  

3 Annotation[][] getParameterAnnotations() //When the formal parameters represented by the array Annotation of the returned array are annotated s, the Executable of the declaration order is represented by the object.  

4 int getParameterCount() //Returns the number of formal parameters (whether explicitly or implicitly declared) of the executable represented by this object.  

5 Class<?>[] getParameterTypes() //Returns an array of class objects that represent the formal parameter types of the executable represented by the object in declarative order. 

```



The method of creating objects is not mentioned first, but later.



2.5 Reflection creates objects and invokes methods

---------------



### 2.5. 1 create objects of ordinary classes



There are two ways to create objects of a common class



**First: call Class Object method**



```

4 //First get the Class object

5 Class clazz=Class.forClass("test.Student");

6 //create object

7 Student stu=(Student)clazz.newInstance();

Note: this method can only create objects of classes without parameter constructors



**Second: through Constructor of newInstance()method**



1 / / create a Class object first

summary

Other contents can be familiar with, studied and digested one by one according to the knowledge points sorted out in the road map. It is not recommended that you read and study. It is best to watch more videos and read the places you don't understand repeatedly. After learning a video content, you must review it the next day, and always form a mind map to form a tree knowledge network structure for future review.

Here is also a very good summary note of Java basic core, which is specially shared with you, If necessary, click here for free

catalog:

Screenshot of some contents:

1 //First create a Class object


# summary

Other contents can be familiar with, studied and digested one by one according to the knowledge points sorted out in the road map. It is not recommended that you read and study. It is best to watch more videos and read the places you don't understand repeatedly. After learning a video content, you must review it the next day, and always form a mind map to form a tree knowledge network structure for future review.

Here's another good one< Java Basic core summary notes ", specially shared with you,[If necessary, click here for free](https://gitee.com/vip204888/java-p7)

**catalog:**

[External chain picture transfer...(img-39lD5Jp2-1628409995886)]

**Screenshot of some contents:**

[External chain picture transfer...(img-plWgOgLR-1628409995888)]

[External chain picture transfer...(img-GwhTgOYg-1628409995889)]

Keywords: Java Back-end Interview Programmer

Added by creatives on Tue, 28 Dec 2021 21:12:37 +0200