The first way of expression (any class has an implicit static member Variable class):
1 Class c1 = Foo.class;
The second representation method (the object of this class is known, through the getClass method):
1 Foo foo1 = new Foo(); 2 Class c2 = foo1.getClass();
The third way of expression
1 Class c3 = null;2 try {3 c3 = Class.forName("com.format.test.Foo");4 } catch (ClassNotFoundException e) {5 e.printStackTrace();6 }
1 try {2 Foo foo2 = (Foo) c1.newInstance(); //需要有无参构造3 } catch (InstantiationException e) {4 e.printStackTrace();5 } catch (IllegalAccessException e) {6 e.printStackTrace();7 }
The above is the detailed content of Tutorial on using reflection Class class. For more information, please follow other related articles on the PHP Chinese website!