Android (Java) Learning Note 106: Reflection mechanism

Source: Internet
Author: User

Reflection mechanism:

The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanisms of the Java language. To dissect a class, you must first obtain the bytecode file object for that class. The anatomy uses the method in class. So first get the object of class type corresponding to each bytecode file.

1  Packagecn.itcast_01;2 3 /*4 * Reflection: It is through the class file object, to use the member variables in the file, construction methods, member methods. 5  * 6 * Person p = new person ();7 * p. Using8  * 9 To Use this, you first have to get the class file object, which is actually the object of class. all class files are objects of  classTen * Class: One * Member variable class field, through the object of the class that represents the member variable, to tune the member variable A * Constructor Method class constructor, through the object of the class that represents the construction method, to tune the construction method - * Member Method class method, through the object that represents the class of the member method, to tune member methods - * That is, class is the abstract summary of other classes, its member variables, construction methods, member methods are the same, is the corresponding abstract summary, naturally also represents a class. class is also a member of a class  the * How to get the class file object: - * GetClass () method for A:object class - * B: Static property class for data type - * Static methods in the C:class class + * public static Class forname (String className) -  *  + * Who do we use in general? A * A: Play any one of your own, the second is more convenient at * B: Development of a third - * Why? Because the third type is a string, not a specific class name. This allows us to configure such a string in the configuration file.  -  */ -  Public classReflectdemo { -      Public Static voidMain (string[] args)throwsClassNotFoundException { -         //Mode 1 inPerson p =NewPerson (); -Class C =P.getclass (); to  +person P2 =NewPerson (); -Class C2 =P2.getclass (); the  *SYSTEM.OUT.PRINTLN (p = = p2);//false $System.out.println (c = = C2);//truePanax Notoginseng  -         //Mode 2 theClass C3 = Person.class; +         //Int.class; A         //String.class; theSystem.out.println (c = =C3); +  -         //Mode 3 $         //ClassNotFoundException, must take the full path of the package name, such as if it is Class C4 = class.forname ("person"); $Class C4 = class.forname ("Cn.itcast_01.Person"); -System.out.println (c = =C4); -     } the}

Android (Java) Learning Note 106: Reflection mechanism

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.