Class type{public int pubintfield; Public String Pubstringfield; private int Prvintfield; Public Type () {Log ("Default Constructor"); } Type (int arg1, String arg2) {Pubintfield = arg1; Pubstringfield = arg2; Log ("Constructor with parameters"); } public void Setintfield (int val) {This.prvintfield = val; } public int Getintfield () {return prvintfield; } private void Log (String msg) {System.out.println ("Type:" + msg); }} class Extendtype extends type{public int pubintextendfield; Public String Pubstringextendfield; private int Prvintextendfield; Public Extendtype () {Log ("Default Constructor"); } extendtype (int aRG1, String arg2) {Pubintextendfield = arg1; Pubstringextendfield = arg2; Log ("Constructor with parameters"); } public void Setintextendfield (int field7) {This.prvintextendfield = Field7; } public int Getintextendfield () {return prvintextendfield; } private void Log (String msg) {System.out.println ("Extendtype:" + msg); } }
1. Get Class object
| Call GetClass |
Boolean var1 = true; class<?> classType2 = Var1.getclass (); System.out.println (CLASSTYPE2); Output: Class Java.lang.Boolean |
| using the. class Syntax |
class<?> classType4 = Boolean.class; system.out.println (CLASSTYPE4); output: Class Java.lang.Boolean |
| Using the static method Class.forName () |
class<?> ClassType5 = Class.forName ("Java.lang.Boolean"); System.out.println (CLASSTYPE5); Output: Class Java.lang.Boolean |
Using the type syntax of primitive wrapper classes This returns the native type, and the Boolean.class returns the different |
class<?> classType3 = Boolean.type; System.out.println (CLASSTYPE3); Output: Boolean |
2, get the fields of the class
| Public Field GetField (String name) |
Returns a Field object that reflects the specified public member field of the class or interface represented by this class object |
| Public field[] GetFields () |
Returns an array containing some field objects that reflect all accessible public fields of the class or interface represented by this class object |
| Public Fieldgetdeclaredfield (Stringname) |
Returns a Field object that reflects the specified declared field of the class or interface represented by this class object |
| Public field[] Getdeclaredfields () |
Returns an array of field objects that reflect all the fields declared by the class or interface represented by this class object |
Visible GetFields and Getdeclaredfields differences:
GetFields returns a property that is declared public, including the definition in the parent class,
Getdeclaredfields returns all defined properties for the specified class definition, excluding the parent class.
3. Get the method of the class
A method of a class is obtained through a reflection mechanism, and then the method that corresponds to an instance of the class is called
The Class<t> class provides several ways to get a class.
| public methodget Method (string name,class<?> ... parametertypes) |
Returns a Method object that reflects the specified public member methods of the class or interface represented by this class object |
| public method[] getmethods () |
return An array containing some method objects that reflect the common member method |
| Public for the class or interface represented by this class object, including those declared by the class or interface, and those inherited from the superclass and the hyper-interface. method Getdeclaredmethod (stringname,class<?> ... parametertypes) |
Returns a Method object that reflects this Class The specified declared method for the class or interface represented by the object |
| public method[] getdeclaredmethods () |
Returns an array of method objects that reflect all methods of the class or interface declaration represented by this class object, including public, protected, default (package) access, and private methods, but not inherited methods |
Java Reflection mechanism