Reflection Class usage, reflection class
Any Class is an instance object of the Class. This instance object has three representation methods.
- The first representation (any class has an implicit static member variable class ):
1 Class c1 = Foo. class;
- The second representation (this class object is known through the getClass method ):
1 Foo foo1 = new Foo (); 2 Class c2 = foo1.getClass ();
※C1 and c2 indicate the class type of the Foo class)
1 Class c3 = null; 2 try {3 c3 = Class. forName ("com. format. test. foo "); 4} catch (ClassNotFoundException e) {5 e. printStackTrace (); 6}※Create an object instance of this class through the class type
1 try {2 Foo foo2 = (Foo) c1.newInstance (); // requires parameter construction 3} catch (InstantiationException e) {4 e. printStackTrace (); 5} catch (IllegalAccessException e) {6 e. printStackTrace (); 7}