Before learning Java just learned the basic grammar operation, the use of various common methods, with the slow learning, many great gods feel to want to become the great God, we must put the reflection of Java to understand, so I took the curious heart to learn exactly what the reflection is, so on the Internet to find information to study, Did not expect the harvest is really not small, in fact, we usually use with multi-operation is through reflection to complete, for example, the servlet in the configuration of Web. XML is the same as the reflection to create a new object, and SSH is through reflection to instantiate the object, through the reflection let me better understand the SSH framework. The next little brother began to talk about the details of the reflection, because I level limited ,
Reflection is actually obtaining information about the type through the class object;
Reflection-related classes
1:class: Represents a class;
2: Field: Represents a member variable;
3: method: Representation,
4: Constructor: representing the constructor
1:class class: There are three ways to get class classes
If you define a users class
Public classUsers {PrivateInteger Id; PrivateString Name; PrivateDate Birth; Private floatscore; PublicInteger getId () {returnId; } @Override PublicString toString () {return"Users [id=" + Id + ", name=" + Name + ", birth=" +Birth+ ", score=" + score + "]"; } PublicUsers () {} PublicUsers (intID) { This. Id =ID; } Public voidsetId (Integer ID) {ID=ID; } PublicString ResultStr () {return"Str"; } PublicString GetName () {returnName; } Public voidsetName (String name) {Name=name; } PublicDate Getbirth () {returnBirth; } Public voidSetbirth (Date birth) {birth=birth; } Public floatGetscore () {returnscore; } Public voidSetScore (floatscore) {Score=score; }}
Then there are three ways to get the class
USERSP = new Users ();
Three ways to get classes
Class C2 = P.getclass ();//Create a new object to get the class again
Class C1 = users.class;//gets the class directly from the static method of the class
Class C3 = Class.forName ("Com.lishun.Peason");//Get the class by package name and class name (the configuration file is the way to get the instance)
Gets the class to get the constructors, methods, and members of the class (in this case, using the third method above to get the class instance)
1: Get constructor: Constructor
Class C3 = Class.forName ("Com.lishun.Peason");//
Get the class and parent class A public constructor
Constructor Cpublic=c3.getconstructor (int.class);//int.class refers to a constructor that contains an int type parameter
Get all public constructors for this class and parent class
Constructor[] Cpublicall=c3.getconstructors ();
Get a private or public constructor for this class
Constructor Cpublicorprivaet=c3.getdeclaredconstructor ();
Get all public or private constructors for this class
Constructor[] Cpublicorprivaetall=c3.getdeclaredconstructors ();
To perform a construction method
Peason p= (Peason) cpublic.newinstance (1);
2: Get Methods: Method
//Get a single public method of this class and parent class
method M=c3.getmethod ("ResultStr");
//gets all the public methods of this class and the parent class
method[] Mall=c3.getmethods ();
//gets a single public or private method of this class (all access levels can be obtained)
method m1= C3.getdeclaredmethod ("ResultStr1", String.class);
//gets all public or private methods of this class (All access levels are available)
method[] Mall1=c3.getdeclaredmethods ( );
//Executes the method
m.invoke (C3.newinstance ());//If static methods are executed , the first parameter is null without a new object
3: Member: Field
Get a single public method for this class and for the parent class
try {
Field f = C3.getfield ("Name");
} catch (Exception e) {
System.out.println (E.getmessage ());
}
Get all public methods for this class and for the parent class
Field[] Fall=c3.getfields ();
Get a single public or private member of this class (All access levels)
Field F1=c3.getdeclaredfield (Name ");
Get all public or private methods of this class (All access levels)
Field[] Fall1=c3.getdeclaredfields ();
Object o=c3.newinstance ();
Assign values to members, private members, methods, constructors are not assignable by default
F1.setaccessible (TRUE);//Set Private members to be available
F1.set (O, "Lishun");
System.out.println (o);
Well, I understand the reflection of this,, the younger brother if there is no mention of the whole or there are errors, hope that the great God, I am also a small rookie in the study, I hope to learn more technical knowledge
The reflection of learning in Java