Today the main thing is to learn a very important knowledge of reflection. My understanding is that we did this by loading the class file and then initializing and finally executing. That is, when a program uses a class, if the class is not loaded into memory, the system initializes the class by loading, connecting, and initializing three steps. And what is reflection, my simple understanding is that the class file is used to obtain information such as the method of the member variable of the classes.
There are three ways to get a bytecode file.
The first object to use the class +.getclass ()
Person p = new person ();
Class C = P.getclass ();
The second type of direct use Class +.getclass ()
Class C3 = Person.class;
The third is to use the class call method (you need to give the path to the classes)
Class C4 = class.forname ("Cn.itcast_01.Person");
In general, we use a third, if we use the path in the forname in the form of read files, then we just need to specify a different path in the file then we can read the different classes.
Then the main way is to use reflection to get the constructor method member variable member method.
Get bytecode File Object
Class C = class.forname ("Cn.itcast_01.Person");
Get Construction method
Public constructor[] GetConstructors (): all common constructor methods
Public constructor[] Getdeclaredconstructors (): All construction methods
Constructor[] cons = C.getdeclaredconstructors ();
for (Constructor con:cons) {
System.out.println (con);
// }
Get a single constructor method
Public constructor<t> getconstructor (class<?> ... parametertypes)
Parameters are: The number of construction parameters you want to get, and the class byte-code file object of the data type
Constructor con = c.getconstructor ();//Returns a constructor method object
Use the constructor method represented by this Constructor object to create a new instance of the Declaration class for the constructed method, and initialize the instance with the specified initialization parameters.
Object obj = con.newinstance ();
System.out.println (obj);
To create an object by using the parameter construction method object
Constructor con = c.getconstructor (String.class, Int.class,string.class);
Public T newinstance (Object ... initargs)
Object obj = con.newinstance ("111", 22, "333");
System.out.println (obj);
Get all the member variables
field[] fields = C.getfields ();
field[] fields = C.getdeclaredfields ();
for (Field field:fields) {
System.out.println (field);
// }
/*
* Person P = new person (); p.address = "Beijing"; SYSTEM.OUT.PRINTLN (P);
*/
Creating an object from a parameterless construction method
Constructor con = c.getconstructor ();
Object obj = con.newinstance ();
System.out.println (obj);
To get a single member variable
Get address and assign value to it
Field Addressfield = C.getfield ("Address");
public void Set (Object obj,object value)
Sets the field that is represented by this field object on the specified object variable to the specified new value.
Addressfield.set (obj, "Beijing"); Set the value for the Addressfield field of the Obj object to "Beijing"
System.out.println (obj);
Get all the methods
Method[] methods = C.getmethods (); To get their own public methods including fathers
Method[] methods = C.getdeclaredmethods (); Get all of your own methods
for (Method method:methods) {
System.out.println (method);
// }
Constructor con = c.getconstructor ();
Object obj = con.newinstance ();
Get a single method and use the
public void Show ()
Public Method GetMethod (String name,class<?> .... parametertypes)
The first parameter represents the method name, and the second parameter represents the class type of the method's argument
Method m1 = C.getmethod ("show");
public object Invoke (Object Obj,object ... args)
The return value is object receive, the first parameter indicates who the object is, and the second parameter represents the actual argument that called the method
M1.invoke (obj); Invoke the M1 method of the Obj object
System.out.println ("----------");
public void Method (String s)
Method m2 = C.getmethod ("method", String.class);
M2.invoke (obj, "hello");
25th Day of Java Learning (reflection)