1:sun provides a class of reflection mechanisms:
Java.lang.class<t>
Java.lang.reflect.constructor<t>
Java.lang.reflect.Field
Java.lang.reflect.Method
Java.lang.reflect.Modifier
2: What is reflection
The Java reflection mechanism is in the running state, for any class. Can all know all the properties and methods of this class, for any one object, can call any of its methods and properties, the dynamic acquisition of information and the function of the method of dynamic invocation of the object is called the Java language reflection mechanism;
3: The effect of the reflection
Anti-compilation. Class--à.java
The reflection mechanism provides access to properties, methods, and construction methods in Java objects.
4: Three ways to create class objects
JavaBean:
Public classPersonImplementschina{PrivateString name; Private intAge ; Private Charsex; PublicPerson () {Super (); } PublicPerson (String name,intAgeCharsex) { Super (); This. Name =name; This. Age =Age ; This. Sex =sex; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } Public CharGetsex () {returnsex; } Public voidSetsex (Charsex) { This. Sex =sex; } Public voideat () {System. println ("Eat It" ); } @Override PublicString toString () {return"Person [name=" + name + ", age=" + Age + ", sex=" + Sex + "]" ; } @Override Public voidSaychina () {//TODO auto-generated Method StubSystem. Out. println ("" + AUTHOR + "Nationality:" +National ); } @Override PublicString SayHello (string name,intAgeCharsex) { //TODO auto-generated Method Stub return"Name:" + name + "Age:" + Ages + "Gender:" +sex; }}
1 Public classClassDemo02 {2 3 Public Static voidMain (string[] args) {4person P1 =NewPerson ("xiaoming", 20, ' Male ' );5person P2 =NewPerson ("Little Red", 23, ' female ' );6 7 //How to create a Class object: (object. GetClass ()), gets the bytecode file in the person class8Class Class1 =P1.getclass ();9 System. Out.println (P1.getclass (). GetName ());TenClass Class2 =P2.getclass (); OneSystem. Out.println (Class1 = =class2); A -System. Out.println ("==============================" ); - //How to create a Class object two: (Class. Class: You need to enter an explicit class, any type has a static class attribute) theClass CLASS3 = person.class; -System. Out.println (Class1 = =class2); - -System. Out.println ("==============================" ); + //How to create a Class object three: (forname (): Just pass in as a string when passing in) - //returns a class object through a forname (String className) static method of class, ClassName must be the full path name; + //Class.forName () has an exception: ClassNotFoundException A atClass CLASS4 =NULL; - Try { -CLASS4 = Class.forName ("Cn.itcast.Person"); -}Catch(ClassNotFoundException e) { - //TODO auto-generated Catch block - e.printstacktrace (); in } -System. Out.println (Class4 = =class3); to } +}
Note: in the development of the general use of the third method, because the third receive is a string path, in the future can be obtained through the configuration file, universal good;
4:newinstance () method---> after you get the class type, you can create objects of that type
Public T newinstance () throws Instantiationexception,illegalaccessexception
1 Public classreflect03 {2 3 Public Static voidMain (string[] args)throwsException {4 5Class C1 = Class.forName ("Com.itheima04.Test_20171106.Test_20171207.Person");6 7 //creates a new instance of the class represented by this class object.8 //The newinstance method calls the empty argument construction method of person9Object o =c1.newinstance ();Ten System.out.println (o.tostring ()); One}
Java Reflection mechanism (three ways to create a class object)