Reflection in Java (j2se entry 20)

Source: Internet
Author: User

Reflection

Reflection, dynamic analysis at runtime or using a class for work.
Class Object,Is an object that describes the class information. When a VM loads a class, it creates a class object for the class and loads the object. Class is the type of Class Object. Only objects are loaded to virtual machines. A class is loaded only once..

Three methods for obtaining class objects: (class objects are obtained without the new method)

1) You can also use the class name. Class to obtain the class object of this class..
2)Use a. getclass () to get the class object of this object type..
3)You can also use class. forname (Class Name) (static method in the class), you can also get the class object of this class, (note that the class name written here must be a fully qualified name (full name ), is the package name and class name, xxx. xxx. XXXX ). This method is often used to force class loading.

The class object of a class is unique.
When class. forname (class name) is used, if the class name written for use is not loaded, the class will be loaded..
Class C, C. getsuperclass () is used to obtain the Class Object of the class's parent class..C. getinterfaces () will obtain the interface implemented by this class. This method returns an array of class objects.

Method objectIs the description of the method information in the class. JAva. Lang. Reflect. Method,The method class object can be passed through the getmethods ()The method obtains an array of method objects and obtains all the method objects defined in the class, except the constructor.

Constructor objectIs used to describe the information of the constructor.Java. Lang. Reflect. ConstructorThe object of the constructor class can be obtained through the getconstructors () method of the Class Object to obtain all the constructor objects of the class..

Attribute objectTo describe the attributes.Java. Lang. Reflect. FieldYou can useClass Object getfields ()This method is used to obtain attribute objects of all attributes..
Reflection can get information about the methods and attributes defined in this class. Simple data types need to be converted into encapsulation classes when using reflection.

Reflection can generate objects and call methods of the class through the Class Object manipulation class.

To construct a Class Object Using Reflection

1. Obtain class objects (class objects are descriptions of class information)
2. Obtain the constructor object (the method object is the description of the method information in the class)
3. Get the object (use the constructor object instead of the constructor method. If you use the constructor without parameters, you can skip 2nd parts and directly use the class object. newinstance () method to obtain the object of this class)
4. Obtain the method object.
5. Call a method using a method object (using the object of this class as the first parameter ).

When a method is called, the parameter table of the method uses an array of class objects to represent the parameter type.

For other reflection methods, refer to the API documentation. Java. Lang. Class, java. Lang. reflce package

Reflection provides the maximum versatility. reflection can bypass the compiler, and reflection can access the private attributes and methods of objects.

Import java. Lang. Reflect .*;

Public class testreflect {
Public static void main (string [] ARGs) throws exception {

// 1.get Class Object
Class C = Class. forname ("student ");
// 2.get constructor object
Class [] cs = {string. Class };
Constructor con = C. getconstructor (CS); // call the formulated construction method according to the parameter table.
// 3. Create object
Object [] OS = {"liucy "};
Object o = con. newinstance (OS );
// 4.get method object
String methodname = "study ";
Class [] PCs = {string. Class };
Method M = C. getmethod (methodname, PCs); // obtain the method object according to the parameter table.
// 5. Invoke the Method
Object [] OCs = {"EJB "};
M. Invoke (O, OCs );

}

}

Class student {
Private string name;

Public student (string N ){
This. Name = N;
}

Public void Study (string course ){
System. Out. println (name + "Studies" + course );
}

Public void eat (string food ){
System. Out. println (name + "Eats" + food );
}
}

Class teacher {
String name;

Public Teacher (string N ){
This. Name = N;
}

Public void teach (string course ){
System. Out. println (name + "teaches" + course );
}
}

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.