What is the knowledge of Java reflection mechanism?
This article mainly explains "what is the knowledge of Java reflection mechanism". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the knowledge of Java reflection mechanism"?
What is the class class
In an object-oriented world, everything is an object. A class is an object, and a class is an instance object of the java.lang.Class class. In addition, only the java virtual machine can new the class class. Any class is an instance object of the Class class. This instance object can be expressed in three ways:
Public class User {} public class ClassTest {User u=new User (); / / Mode 1: Class c1roomUser.class; / / Mode 2: Class c2=u.getClass (); / / Mode 3: Class c3=Class.forName ("com.forezp.User"); / / you can create an instance object of the class User user= (User) User.class () through the type of the class;}
II. Dynamic loading of class
Class.forName (the full name of the class); this method represents not only the type of the class, but also the dynamically loaded class. The compile-time load class is statically loaded, and the run-time load class is dynamically loaded.
Third, obtain method information
The basic data types and the void keyword are instances of the Class class; you can get the name of the class through getame (); getSimpleName ().
Class c1roomString.class; Class c2roomint.class; Class c3void.class; System.out.println (c1.getName ()); System.out.println (c2.getSimpleName ())
Get all the methods of the class and print them out:
Public static void printClassInfo (Object object) {
Class c=object.getClass ()
System.out.println ("name of class:" + c.getName ())
/ * *
* A member method is a method object
* getMethod () all public methods, including public inherited by the parent class
* getDeclaredMethods () gets all the methods of the class, including private, but not the inherited methods.
, /
Method [] methods=c.getMethods (); / / get method
/ / get all the methods, including private, c.getDeclaredMethods ()
For (int iTuno Bandi)