(Program this season
Java's reflection mechanism
Comprehensive explanation of factory mode
1. What is reflection
Under the Java.lang.reflect package
Under normal circumstances, we can instantiate a target through a class, then through reflection, we can actually get such a complete package through a target. Class name.
packageorg.michael;
classPerson{
privateStringname;
privateintage;
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo01{
publicstaticvoidmain(Stringargs){
Personp=newPerson();
//Assuming that you don't know that p is the target of that class, you can find it through the reflection mechanism
ClaS/SC=null;
c=p.getClass();
System.out.println(c.getName());
}
}
Take a look at the effect:
In addition to the package class name that can find the target location, it is also possible to actually list all the method names.
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo02{
publicstaticvoidmain(Stringargs){
Personp=newPerson();
//Assuming that you don't know that p is the target of that class, you can find it through the reflection mechanism
ClaS/SC=null;
c=p.getClass();
Methodm=c.getMethods();
for(inti=0;ilt;m.length;i ){
System.out.println(m);
}
}
}
2. Research Class
The structure method of the Class class is private and cannot be seen directly from the outside, so there must be a method inside it to obtain the Class instance.
publicstaticClasslt;?gt;forName(StringclassName)throwsClassNotFoundException
This method can return an instance of the Class class, this method receives a complete package. Class name.
Through the newInstance method, the incoming complete string (package.class name) can be instantiated.
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo03{
publicstaticvoidmain(Stringargs){
Personp=null;
ClaS/SC=null;
try{
c=Class.forName(org.michael.Person);
}catch(Exceptione){}
try{
p=(Person)c.newInstance();
}catch(Exceptione){}
//The above two lines of code can also be replaced by the following line of code~
//p=(Person)Class.forName(org.michael.Person).newInstance();
p.setName(Michael);
p.setAge(30);
System.out.println(p.getName() ---gt; p.getAge());
}
}
If you want to use the above code to instantiate a target, there must be a future condition: there must be a parameterless structure method in the class of the target site, if there is no such parameterless structure, an error will definitely appear.
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicPerson(Stringname,intage){
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo04{
publicstaticvoidmain(Stringargs){
Personp=null;
ClaS/SC=null;
try{
c=Class.forName(org.michael.Person);
p=(Person)c.newInstance();
}catch(Exceptione){
System.out.println(e);
}
System.out.println(p.getName() ---gt; p.getAge());
}
}
At this moment, if you want to continue to instantiate through this operation as the target, you can end it through the structure method class (Constructor).
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicPerson(Stringname,intage){
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
public)