Fanshe.java
Package example5;
Class fanshe{
/*1. Used in some high-versatility code.
The framework behind the study, most of which is implemented by the application framework.
In framework development, they are based on configuration files. A class is configured in a configuration file that can be reflected to get all the contents of a class, which can be performed by a method in the class.
* All content in class: attribute, no parameter construction method, parameter construction method, common method.
* Principle of Reflection:
* The code is saved to the local hard disk in the. java file and compiled with a. class file and loaded into the memory-->class class using the class loader in the JVM (bytecode file representation in memory)
* If I get this class, I can get all the content in the class
* Use reflection first need to get class (three ways)
* (1) class name.
* (2) object name. GetClass ()
* (3) Use Class.forName ("Path to Class")
* Attributes are obtained through a class field.
* Construction method is obtained by a class constructor.
* Common methods are obtained by means of a class method.
*/
private String name;
int age;
method of constructing without parameters
Public Fanshe () {}
A method of constructing parameters
Public Fanshe (String name, int.) {
Super ();
THIS.name = name;
This.age = age;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
}
Testfanshe.java
Package example5;
Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Import Org.junit.Test;
Class Testfanshe {
public static void Main (string[] args) throws Exception {
Three ways to get class classes
Class C1 = Fanshe.class;
Class C2 = new Fanshe (). GetClass ();
Class C3 = Class.forName ("Example5.fanshe");
/*test1 ();
Test2 ();
Test3 (); */
Test4 ();
}
A method for constructing the reflection operation without parameters
public static void Test1 () throws Exception {
Class C3 = Class.forName ("Example5.fanshe");
Get an example of a reflection class
Fanshe f1 = (fanshe) c3.newinstance ();
To invoke a method in a class with an instance
F1.setname ("Baojian");
System.out.println (F1.getname ());
}
A method of constructing parameters of reflection operation
@Test
public static void Test2 () throws Exception {
Get class
Class C3 = Class.forName ("Example5.fanshe");
Get the constructor method class with parameters
The parameter type is passed in the constructor method with the parameter, and is passed as class type.
Constructor con = c3.getconstructor (String.class, Int.class);
Set the value of an instance by using the constructor method of a parameter
Fanshe P1 = (fanshe) con.newinstance ("Aso", 20);
System.out.println (P1.getname () + "" + p1.getage ());
}
Properties of the Reflection action class
public static void Test3 () {
Get class
try {
Class C2 = class.forname ("Example5.fanshe");
Get an instance of a class
Fanshe P2 = (fanshe) c2.newinstance ();
Get Name property, parameter is property name
Field f1 = C2.getdeclaredfield ("name");
Represents the Allow operation of private properties
F1.setaccessible (TRUE);
Set the value for the Name property, the first argument is an instance of the class, and the second parameter is the value to set.
F1.set (P2, "Wangwu");
Get the property value and print the output
System.out.println (F1.get (p2));
} catch (Exception e) {
E.printstacktrace ();
}
}
Using reflection to manipulate common methods
public static void Test4 () throws Exception {
Get class
Class c4=class.forname ("Example5.fanshe");
Get an instance of a class
Fanshe p4= (Fanshe) c4.newinstance ();
The method that gets the class of the common method, the first parameter is the name of the normal method to manipulate, and the second parameter is the type of the parameter passed in the normal method.
Method F4=c4.getdeclaredmethod ("Setage", Int.class);
The operation is private method
F4.setaccessible (TRUE);
This function is to enable the normal method to run and set the value. The first parameter is an instance of the class, and the second parameter is the value to set.
F4.invoke (p4,15);
Prints the value of the setting to print out
System.out.println (P4.getage ());
}
/* Note: When the method of operation is a static method, because the static method is called by the class name. Method Name
* So you don't need an instance of the class
* The first parameter in the Invoke () method is directly written as null
* F4.invoke (null,15);
*/
}
Basic use of reflection in Java