[Original] Java Reflection Technology (2)

xiaoxiao2021-03-06  56

Lesson: 2 Treatment Object 1.creating Objects Usually, create an object with the following method Rectangle R = new Rectangle (); but if you are developing a development tools, you may not know the class to generate an object before running. So to create an object like this: string classname;

/ /.. Load classname from the user interface

Object o = new (classname); // Wrong!

But the above is wrong. The correct way is to use the reflection characteristics of the class:

1) using no-argument constructors, for example: class classdefinition = class.forname (classname); // Specify the running period of the class Object = classdefinition.newinstance (); // Call the unintegrated constructor to generate an instance of the specified class.

2) Using Constructors That Have Arguments This technology is to be used as follows: A. Create a Class object B. Create a constructor object, getConstructor (Class [] params) method, the parameter is a suitable class array that is suitable for constructor. The NewInstance method is called on the Constructor object to generate an object, and the parameter is an Object array equipped with this constructor.

For example: Import java.lang.reflect. *; Import java.awt. *;

Class SampleInstance {

Public static void main (String [] args) {

Rectangle Rectangle; Class Rectangledefinition;

Class [] INTARGSCLASS = new class [] {int.class, int.class}; integer height = new integer (12); integer width = new integer (34); object [] idargs = new object [] {height, width }

Constructor INTARGSCONSTRUCTOR;

Try {// 1. Rectangledefinition = Class.Forname ("java.awt.rectangle"); // 2. INTARGSCONSTRUCTOR = Rectangledefinition.getConstructor (intargsclass); // Find specified constructor //3. Rectangle = (Rectangle) createObject (intArgsConstructor, intArgs); // constructor described objects, object []} catch (ClassNotFoundException e) {System.out.println (e);} catch (NoSuchMethodException e) {System.out.println (e);} }

Public Static Object CreateObject (Constructor Construction, Object [] arguments) {

System.out.println ( "Constructor:" constructor.toString ()); Object object = null; try {object = constructor.newInstance (arguments); System.out.println ( "Object:" object.toString () ); return object;} catch (InstantiationException e) {System.out.println (e);} catch (IllegalAccessException e) {System.out.println (e);} catch (IllegalArgumentException e) {System.out.println ( e);} catch (invocationtargetexception e) {system.out.println (e);} return object;}}

2. Getting Field Values ​​if you are Writing a development Tool Such as a debugger, you must be able to obtain Field Values. This is a three-step process: If you want to make a development tool like Debugger, you have to find filed values The following is three steps: a. Create a Class object b. Create a field object with GetField C. Call the field.getxxxx (Object) method (xxx is int, float, if it is an object; Object means instance) .

For example: Import java.lang.reflect. *; Import java.awt. *;

Class sampleget {

Public static void main (string [] args) {Rectangle R = New Rectangle (100, 325); PrintHeight (R);

}

static void printHeight (Rectangle r) {Field heightField; Integer heightValue; Class c = r.getClass (); try {heightField = c.getField ( "height"); heightValue = (Integer) heightField.get (r); System. Out.println ("HEIGHT:" HeightValue.Tostring ());} catch (nosuchfieldException e) {system.out.println (e);} catch (securityExcect e) {system.out.println (e);} catch (IllegaCcessException E) {system.out.println (e);}}}

3. Setting Field Values ​​a. Create a Class object b. Create a Field object with GetField C. Call the field.Set (Object, WithParam) method (xxx is int, float, if it is an object; Object refers to the instance, withparam finger And the fields with this field. IMPORT JAVA.LANG.REflect. *; Import java.awt. *;

Class sampleSet {

Public static void main (string [] args) {Rectangle R = New Rectangle (100, 20); System.out.println ("Original:" R.TOString ()); ModifyWidth (R, New Integer (300)) System.out.println ("Modified:" R.toString ());

static void modifyWidth (Rectangle r, Integer widthParam) {Field widthField; Integer widthValue; Class c = r.getClass (); try {widthField = c.getField ( "width"); widthField.set (r, widthParam);} catch (NosuchfieldException E) {system.out.println (e);} catch (illegalaccessexception e) {system.out.println (e);}}}

4. Call the specified method a. Create a Class object b. Create a method object Method, getMethod (String methodname, class []) method c. Method object, method.invoke (object, object []), two parameters, One is the object belonging to the method, the second is the list of passed value objects.

The sample program that follows shows you how to invoke a method dynamically. The program retrieves the Method object for the String.concat method and then uses invoke to concatenate two String objects. //

Import java.lang.reflect. *;

Class sampleinvoke {

Public static void main (String [] args) {string firstword = "hello"; // Specify the instance of the class

String secondword = "Everybody."; //

String Bothwords = append (firstword, secondword); system.out.println (Bothwords);

转载请注明原文地址:https://www.9cbs.com/read-118118.html

New Post(0)