Home  >  Article  >  Java  >  Tutorial on using reflection Class class

Tutorial on using reflection Class class

PHP中文网
PHP中文网Original
2017-06-20 16:59:501132browse
Any class is an instance object of the Class class. This instance object has three ways of expression
  • 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();
 ※ c1 and c2 represent the class of Foo class Type (class type)
  • 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 }
 ※ Create the class through the type of the class Object instance
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn