When it comes to reflection, let's start by talking about classes and objects in Java.
In Java everything is object (there are two exceptions, one is a normal data type, the other is static, something that is static is not an object, it belongs to a class).
In Java, the class is also an object, and the class is an instance object of the Java.lang.class class, the so-called there is a class named class.
The following code illustrates three ways to express an instance object of the class class in Java
PackageOrg.guyezhai.reflect; Public classClassDemo1 { Public Static voidMain (string[] args) {//how Foo objects are representedFoo foo1 =NewFoo (); //Foo This class is also an instance object, the Class class instance object//Any class is an instance object of class, and this instance object is represented in three ways//The first way, any class has an implied static member variable classClass C1 = Foo.class; //The second expression, already known to the class object, is through the GetClass () methodClass C2 =Foo1.getclass (); //official website, C1, C2 represents the class type of the Foo class ( class type)//Everything is object, and the class is also an instance object of class, called the class type of the class .//both C1 and C2 represent the class type of the Foo class, and a class can only be an instance object of classSYSTEM.OUT.PRINTLN (C1 = = C2);//true//The Third Way of expressionClass C3 =NULL; Try{C3= Class.forName ("Org.guyezhai.reflect.Foo"); } Catch(ClassNotFoundException e) {e.printstacktrace (); } System.out.println (C2= = C3);//true//We can create an object instance of the class by the class type of the class: Create an instance of Foo from C1, C2, C3 Try{foo Foo= (Foo) c1.newinstance ();//need to have a parameterless constructorFoo.print (); } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } }}classFoo {voidprint () {System.err.println ("Foo"); }}
Class loading in Java is divided into static load class and dynamic load Class Two, first a static load class example:
PackageOrg.guyezhai.reflect; Public classOffice {/** * @paramargs*/ Public Static voidMain (string[] args) {if("Word". Equals (Args[0])) { //The new object is a statically loaded class that needs to load all the classes that might be used at compile time//the problem can be resolved by dynamically loading the classWord W =NewWord (); W.start (); } if("Excel". Equals (Args[0])) { //Excel class does not exist, compilation does not pass//Excel e = new Excel (); //E.start (); } } }
This loading method is common to us, but there is a problem that if a word class or Excel class has one that does not exist, it will compile without passing, causing the entire program to fail to run.
In some cases this may not be what we want, and we want to use it as long as either Word or Excel exists, which needs to be dynamically loaded with the following code:
First create an interface officeable:
Package Org.guyezhai.reflect; Public Interface officeable { publicvoid start ();}
Then let the Word and Excel classes implement the Officeable interface:
package Org.guyezhai.reflect; public class Word implements officeable { public void start () {System.out.printl N ( "Word...start ()" ); } }
Package Org.guyezhai.reflect; Public class Implements officeable { publicvoid start () { System.out.println("Excel...start ( )"); } }
This lets you load Word or Excel dynamically:
PackageOrg.guyezhai.reflect; Public classOfficebetter {/** * @paramargs*/ Public Static voidMain (string[] args) {Try { //dynamically loading classes, loading at run timeClass C = Class.forName (args[0]); //create an object of this class through the class typeOfficeable OA =(officeable) c.newinstance (); Oa.start (); } Catch(Exception e) {e.printstacktrace (); } } }
Java programming--reflection reflect