"Java Basics Summary" reflection

Source: Internet
Author: User

Tag: The underlying string instance determines the red int name to get the containing

1. What is Reflection
Class, Method, Field, Constructor, which are reflection objects. They are classes, methods, member variables, constructors, in-memory form.
The object of Everything! A class is a type, a method is a type, a member variable is a type, a constructor is also a type.
Class can be regarded as the meta-god of the method, which is the meta-god of the methods; field is the meta-god of the Members; constructor is the meta-god of the constructor.
You get the meta-God to manipulate the host.

2. Get the Class object
class represents a type, that is, the type that describes the class.
* Class name. Class:class C1 = Object.class;
* object. GetClass (): Class C2 = Obj.getclass ();
* Class.forName ("full Name"): Class C3 = Class.forName ("java.lang.String");

Use of class:
* GetName (): Get class Name
* Class Getsupperclass (): Gets the parent class type, which is the meta-god that gets the parent class
* class[] getinterces (): Gets the type of all implemented interfaces, that is, gets all the meta-gods that implement the interface.
* T newinstance (): Creates an instance of the class (created by the default constructor and throws an exception if the type does not have a default constructor).

3. Get the constructor class object by class
* constructor[] GetConstructors (): Gets all the public constructors in this class
* Constructor getconstructor (Class ... parametertypes): Gets the public constructor object of the specified parameter type
* constructor[] getdeclaredconstructors (): Gets all constructors in this class, including all access levels
* Constructor getdeclaredconstructor (Class ... parametertypes): Gets the constructor for the specified argument type, containing all access levels

Use of Construtor:
* T newinstance (Object ... params): Call constructor

4. Get the method class object by class
* method[] GetMethods (): Get all public methods in this class and parent class
* Method GetMethod (String name, Class ... parametertypes): Gets the public method of the specified name and parameter type, including the public method in the parent class
* method[] Getdeclaredmethods (): Gets all the methods of this class, including all access levels;
* Mehtod Getdeclaredmethod (String name, Class ... parametertypes): Gets the specified name and parameter type, including all access levels, but does not contain methods from the parent class.

Use of method:
* Object Invoke (Object obj, Object ... params): Invokes a method where obj is the current object and Parmas is a parameter. If the current method is a static method, the first argument is null when the Invoke method is called.

5. Get field class objects by class
* field[] GetFields (): Gets all the public member variables in this class and in the parent class
* Field GetField (String name): Gets the public member variable of the specified name in this class or parent class
* field[] Getdeclaredfields (): Gets the member variables for all access levels in this class, excluding the parent class.
* Field Getdeclaredfield (String name): Gets the member variable of the specified name in this class, which can be all access levels, excluding the parent class.

Use of field:
* void set (object obj, Object value): Assigns a member variable assignment to the specified object, obj is the current object, value is
* Object get (Object obj): Gets the value of the member variable for the specified object, and obj is the current object.

Special case of Reflection: introspection

Introspection is used to manipulate the properties of JavaBean, JavaBean is a special class that is used primarily to pass data. Properties are determined by getter and setter methods, even if there are no fields, as long as the getter and setter methods are attributes.

"Java Basics Summary" 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.