Understanding of Java Reflection

Source: Internet
Author: User

Reflection: Gets the class at which the object runs.
Person.class-->person Object Class.class-->class Object
We create a person object from a Person.class bytecode file to represent a man in the real world. The person object is used to map a human object, which is not a bytecode file on a hard disk, but rather is created in memory based on the load class. What we call a load class is actually an object of class type. The inside contains only static members.
We create a class object from the Class.class bytecode file to represent a real-world category, which is used to map a class, which is not a bytecode file on a hard disk, but instead encapsulates the bytecode information in memory as an object, which is the class object. This object maps a class.
------------------------------------------------------------------
The complete process for person p=new person () is this: first find the Person.class bytecode file on your hard disk. The bytecode information is then encapsulated in memory as a class object, which is the type of the class object, called the Load class, and then creates the person object from the class object.

Note: Each object (the new object) corresponds to a Class object (load Class), and different class objects represent data types that are not used in Java. All classes in Java are encapsulated as class objects.

public static void Main (string[] args) throws Exception {//Person.class-to Person object--zhangsan//class.class--& Gt Class object (representing a type)--Person.classobject obj = new Person ("Zhangsan", 19),//The type of object being obtained is/*1 in three ways. If you get the object class Clazz =   Obj.getclass (); Returns an instance object of type class. String classname = Clazz.getname (); System.out.println (classname) *//*2. If you get a string that represents the class name string classname = "Cn.itcast.day9.reflectstudy.Person"; Class clazz = class.forname (classname); System.out.println (Clazz.getname ()); *//*3. Clearly know what type of class object to take class clazz = Person.class; System.out.println (Clazz.getname ()); System.out.println (Int.class.getName ()); */class clazz = Obj.getclass ();/* traverse all properties of the class field[] fields = Clazz.getdeclaredfields (); for (int i=0; i<fields.length; i++) {String name = Fields[i].getname (); Class type = Fields[i].gettype (); System.out.println ("Property name:" + name + "attribute type:" + type.getname ());} *//* gets a property that accesses this property field field = Clazz.getdeclaredfield ("name"); Field.setaccessible (true);//cancels the access check Field.set (obj, " Brother Zeng "); System.ouT.println (obj); Object value = Field.get (obj); System.out.println (value); *//* gets the Run method of the object, with a value of method M = Clazz.getdeclaredmethod ("Run", null); M.invoke (obj, null); *// * Get the constructor method of the class, create an instance of the class */constructor con = Clazz.getconstructor (String.class, Int.class); Object object = Con.newinstance (" Zhangsan ", 29); Person P = (person) object;  SYSTEM.OUT.PRINTLN (P);}


Understanding of Java Reflection

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.