[Original] Java Reflection Technology (1)

xiaoxiao2021-03-06  60

Java's reflection technology is very powerful, organizing some information! !

(For reprint, please indicate the source!)

Lesson: Detection EXAMING CLASS

1. RETRIEVING CLASS Objects Gets a Class object (Metadata)

A, get from an instance of an object. Class c = mystery.getClass (); // ((Return Class) B, acquiring textfield t = new textfield (); class c = t.getClass (); C = c.getusuperclass (); c , Know the class name, you can get .class to get the name after the name. Class C = java.awt.button.class; d, if the class name is unknown at compile, you can use the class.forname () method to get .class c = class.Forname (ClassString);

2.Getting the class name Get class name C.GetName ();

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

Class Samplename {

Public static void main (string [] args) {button b = new button (); printname (b);}

Static void printname (Object O) {class c = o.getClass (); string s = c.getname (); system.out.println (s);}}

3. Discovering Class Modifiers Retrieves the modifier a. Get a integer identifier value via the getModifiers () method. b. Judging this value via Java.Reflect.Modifier object's ISPUBLIC, Isabstract, and IsFinal method.

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

Class SampleModifier {

Public static void main (string [] args) {string s = new string (); printmodifiers (s);}

Public Static Void PrintModifiers (Object O) {Class C = O.getClass (); INT M = C.getModifiers (); if (Modifier.Ispublic (m)) System.out.Println ("public"); if (Modifier); IF .issabstract (m)) System.out.println ("Abstract"); if (Modifier.isfinal (m)) System.out.println ("Final");}}

4.Finding SuperClasses Checking the father class, for example: import java.lang.reflect. *; Import java.awt. *;

Class Samplesuper {

Public static void main (string [] args) {button b = new button (); printsuperclasses (b);}

static void printSuperclasses (Object o) {Class subclass = o.getClass (); Class superclass = subclass.getSuperclass (); while (superclass = null!) {String className = superclass.getName (); System.out.println (className ); Superclass = superclass; superclass = subsclass.getsuperclass ();}}} 5.Identifying the interfaces mails} 5.IDentifying the interfaces implementing by a class Retrieving the interface for the specified class, for example: import java.lang.reflect. *; Import java.io. *;

Class sampleInterface {

Public static void main (string [] args) {try {randomaccessfile r = new randomaccessfile ("myfile", "r"); printinterfacenames (r);} catch (ioException e) {system.out.println (e);} }

Static void printinterfacenames (object o) {class c = o.getClass (); class [] theinterface = c.GetInterface (); for (int i = 0; i

Import java.lang.reflect. *; import java.util. *;

Class SampleCheckInterface {

Public static void main (string [] args) {class thread = thread.class; class runnable = runnable.class; verifyInterface (thread); verifyinterface (runnable);}

Static void verifyinterface (Class C) {string name = c.getname (); if (C.ISinterface ()) {system.out.println (Name "is an interface.");} else {system.out.println (Name "is a class.");}}}

Suisi: c.isinterface ()

7.Identifying class Fields Find out specified class All Domain Members Each data can use java.reflect.field to enclose its name, type, and modifier. You can also acquire or set to the value of the member through the corresponding method. Such as: Import java.lang.reflect. *; Import java.awt. *; Class samplefield {

Public static void main (string [] args) {gridbagconstraints g = new gridbagconstraints (); printfieldnames (g);}

Static void printfieldnames (object o) {class c = o.getClass (); field [] publicfields = c.Getfields (); for (int i = 0; i

8. Discovering Class Constructors Retrieves the constructor of the specified class

When an instance of a class is created, this method can be overloaded by a method of searching. Each constructor can be described by class constructor, including name, modifier, parameter type (class []), and exception list. You can get the constructor array of this class through a Class's getConstructors method. Routines: import java.lang.reflect. *; Import java.awt. *;

Class sampleconstructor {

Public static void main (string [] args) {Rectangle R = new Rectangle (); showconstructors (r);}

Static void showconstructors (Object O) {Class C = O.getClass (); Constructor [] theconstructors = c.getconstructors (); for (int i = 0; i

Routine:

Import java.lang.reflect. *; import java.awt. *;

Class SampleMethod {

Public static void main (string [] args) {polygon p = new polygon (); showMethods (p);}

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

New Post(0)