1. What is the reflection of Java?
The reflection mechanism of Java is not sure which class is loaded at compile time, but only loading, detecting and self-auditing when the program is running. Classes that are not known at compile time are used. Such a compilation feature is Java reflection.
2. class
Using the Java reflection mechanism, you have to use the Java.lang.Class class. When a class is loaded, the Java Virtual machine automatically produces a class object. This class object allows us to obtain information such as the methods, members, and declarations and definitions of the constructor method that are loaded into the virtual machine.
3. How can I use the Reflection API to know the information of a class when running?
Let's pass the following code example:
Package com.lovo.exercise02;
/**
* Card Class
*/
Public classCard {Private intplayernumber;//number of playersPrivate intcardnumber;//the number of cards for each personPrivateString[] Huase = {"Red Peach","Black Peach","blocks","Plum Blossom" }; PrivateString[] Paivalue = {"A","2","3","4","5","6","7","8","9", "Ten","J","Q","K" }; PrivateList<string>CARDS;//stands for 52 cards/**
* Constructors with two parameters
*/ PublicCard (intPlayernumber,intCardnumber) {super (); This. Playernumber =Playernumber; This. Cardnumber =Cardnumber; Cards=NewArraylist<string>(); for(String hs:huase) { for(String pv:paivalue) {Cards.add (HS+PV); } } } /**
* Number of Cards acquired
*/ PublicList<string>getcards () {return This. Cards; }/**
* Shuffle
*/ Public voidXipai () {collections.shuffle (cards); } /**
* Licensing
*/
Public voidFapai () {System. out. println ("Card:"+cards.size ()); List<String> sublist = Cards.sublist (0, Cardnumber); for(String card:sublist) {System. out. println (card); } sublist.clear (); }}
Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Modifier;
/**
* Test class
*/
Public classTest { Public Static voidMain (string[] args) throws Exception {//Load Com.lovo.exercise02.Card Class clazz = Class.forName ("Com.lovo.exercise02.Card"); //System.out.println (Clazz.getname ()); //System.out.println (Clazz.getpackage ()); //Creating Objects// Object obj = clazz.newinstance (); //Call the parameterless construction method to create an object Constructor Constructor = Clazz.getconstructor (Int.class, int.class);//call has a parameter constructor method Object obj = Constructor.newins Tance (3, 17); //Call method Xipai Fapai//Method Xipai = clazz. GetMethod ("Xipai");//Xipai. Invoke (obj);//Method Fapai = clazz. GetMethod ("Fapai");//Fapai.invoke (obj);Method display = Clazz.getmethod ("Display",int.class, String.class); Object result= Display.invoke (obj,5,"ABC"); System. out. println (Result); //Get Constructor//constructor[] constructors = clazz.getdeclaredconstructors (); //For (Constructor constructor:constructors) {//System.out.print (Constructor.getname () + "("); //class[] types = Constructor.getparametertypes (); //For (Class class1:types) {//System.out.print (Class1.getname () + ","); // } //System.out.println (")"); // } //get field//field[] fields = Clazz.getdeclaredfields (); //For (Field field:fields) {//System.out.println (Field.gettype (). GetName () + "" +//field.getname ()); // } //Get Public method//method[] methods = Clazz.getmethods (); //method[] methods = Clazz.getdeclaredmethods (); //For (Method method:methods) {//System.out.print (modifier.tostring (Method.getmodifiers ()) + "" +//Method.getreturntype (). GetName () + "" + method.getname () + "("); //class[] parametertypes = Method.getparametertypes (); //For (Class class1:parametertypes) {//System.out.print (Class1.getname () + ","); // } //System.out.println (")"); // } }}
The reflection mechanism of Java