java reflection Comments
This article is still a small example to illustrate, because I always feel, driving is the best case, or else look at the theory, then, saw did not understand, but suggest that you read the article, in the back too far to go look at the theory, there will be better understood.
start following text.
[Case 1] through an object to obtain the full package name and class name
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package Reflect; / ** * through an object to obtain the full package name and class name ** / class Demo { / / other codes ... } class hello { public static void main (String [] args) { Demo demo = new Demo (); System.out.println (demo.getClass (). getName ()); } } |
[Run Results]: Reflect.Demo
add one: All objects of the class are actually Class instance.
[Case 2] instantiate Class class object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package Reflect; class Demo { / / other codes ... } class hello { public static void main (String [] args) { Class null ; Class null ; Class null ; try { / / generally try to use this form demo1 = Class.forName ( "Reflect.Demo" ); } catch (Exception e) { e.printStackTrace (); } demo2 = new Demo (). getClass (); demo3 = Demo. class ; System.out.println ( "class name" + demo1.getName ()); System.out.println ( "class name" + demo2.getName ()); System.out.println ( "class name" + demo3.getName ()); } } |
[Run Results]:
class name Reflect.Demo
class name Reflect.Demo
class name Reflect.Demo
[Case 3] Through Class instantiate objects of other classes
through the no-arg constructor to instantiate an object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package Reflect; class Person { public String getName () { return name; } public void setName (String name) { this . name = name; } public int getAge () { return age; } public void setAge ( int age) { this . age = age; } @ Override public String toString () { return "[" + this . name + "" + this . age + "]" ; } private String name; private int age; } class hello { public static void main (String [] args) { Class null ; try { demo = Class.forName ( "Reflect.Person" ); } catch (Exception e) { e.printStackTrace (); } Person per = null ; try { per = (Person) demo.newInstance (); } catch (InstantiationException e) { / / TODO Auto-generated catch block e.printStackTrace (); } catch (IllegalAccessException e) { / / TODO Auto-generated catch block e.printStackTrace (); } per.setName ( "Rollen" ); per.setAge ( 20 ); System.out.println (per); } } |
[Run Results]:
[Rollen 20]
But note, when we put Person of the default no-argument constructor canceled when, for example, have their own definition defines only one argument constructor function, an error occurs:
example, I define a constructor:
1 2 3 4 | public Person (String name, int age) { this . age = age; this . name = name; } |
then continue running the above program will be:
java.lang.InstantiationException: Reflect.Person
at java.lang.Class.newInstance0 (Class.java: 340)
at java.lang.Class.newInstance (Class.java: 308)
at Reflect.hello.main (hello.java: 39)
Exception in thread "main" java.lang.NullPointerException
at Reflect.hello.main (hello.java: 47)
so we later written using Class instantiate objects of other classes, it must define their own no-argument constructor
[Case] Through Class call other class constructor (in this way can also be created through the Class objects of other classes)
package
Reflect;
import
java.lang.reflect.Constructor;
class
Person {
public
Person () {
}
public
Person (String name) {
this
. name = name;
}
public
Person (
int
age) {
this
. age = age;
}
public
Person (String name,
int
age) {
this
. age = age;
this
. name = name;
}
public
String getName () {
return
name;
}
public
int
getAge () {
return
age;
}
@ Override
public
String toString () {
return
"["
+
this
. name +
""
+
this
. age +
"]"
没有评论:
发表评论