Home > Java > javaTutorial > body text

Tutorial on using reflection Class class

PHP中文网
Release: 2017-06-20 16:59:50
Original
1198 people have browsed it
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();
Copy after login
 ※ 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 }
Copy after login
 ※ 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 }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template