A shallow reading of the Java reflection mechanism

Source: Internet
Author: User

Definition: 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 method of an object is called the reflection mechanism of the Java language. (In a running state, you can know the properties and methods of any class, and you can call the properties and methods of any class)

Role:
Determine the class to which any object belongs at run time;
Constructs an object of any class at run time;
Determine the member variables and methods of any class at run time;
A method of invoking any object at run time;
Generates a dynamic agent.

Method:

 

One: Get the full package name and class name of an object
Call class's GetName ()
Cases:
TestClass C = new TestClass ();
System.out.println ("-----------" +c.getclass (). GetName ());
Two: Instantiate class:
1. Usegetclass (), each class has a getclass ()
Class C3 = New TestClass (). GetClass ();
2.classes. class
Class C1 = Testclass.class;
3.class.forname ("Full path class name")
Class C2 =class.forname ("Cn.com.prac.test.TestClass");
Three: Gets the parent class of an object and implements the interface
Parent class:class object. Getsuperclass (). GetName ()
Implementing the interface:Class.getsuperclass (). Getinterfaces ()
Four: Assigning a value to a class's properties or construction methods
Class.newinstance (): Get an instance of a class specific object
Class.getconstructors (): Get all construction methods for a class
Class C = class.forname ("cn.com.prac.pojo.Student");
Student s = (Student) c.newinstance ();
S.setaddress ("Chongqing");
S.setage (20);
S.setid ("5");
S.setname ("John Doe");
Get all construction methods
Constructor[] con =c.getconstructors ();
Student S1 = (Student) con[0].newinstance ();
Student s2 = (Student) con[1].newinstance ("6", 10, "Zhang San", "Chongqing");

System.out.println ("----------" +s2.getaddress () + "---" +s2.getid () + "----" +s2.getname () + "---" +s2.getage ());
Five: Get all the properties of an object
Class C = class.forname ("cn.com.prac.pojo.Student");
field[] fs =c.getdeclaredfields ();
for (Field Field:fs) {
System.out.println ("---------" +field.getname () + "---" +field.gettype (). GetName ());
}
Six: Get all the methods of an object
class.getmethods ();

Usage scenarios: such as: Factory method

  

A shallow reading of the Java 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.