The reflection mechanism of Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.