The method is obtained by reflection:
Method keyword |
Meaning |
Getdeclaredmethods() |
Get all the methods |
Getreturntype() |
Get the return type of the method |
Getparametertypes() |
Get the incoming parameter type for a method |
Getdeclaredmethod ("Method name", Parameter type. Class,......) |
Get a specific approach |
|
|
Constructor method keyword |
Meaning |
Getdeclaredconstructors() |
Get all the construction methods |
Getdeclaredconstructor (parameter type. Class,......) |
Get a specific construction method |
|
|
Parent class and Parent interface |
Meaning |
Getsuperclass() |
Gets the parent class of a class |
Getinterfaces() |
Get an interface for a class implementation |
code example:
@Test Public voidFun ()throwsclassnotfoundexception{//Building ClassesClass<?> cls = Class.forName ("Fanshe. User "); //Get Propertiesfield[] Fields =Cls.getdeclaredfields (); for(Field field:fields) {System.out.println (modifier.tostring (Field.getmodifiers ())+ "\ n" +//get the property's adornmentsField.gettype (). Getsimplename () + "\ n" +//get the type of the propertyField.getname ()//get the name of the property ); } //Get the methodMethod[] Methods =Cls.getdeclaredmethods (); for(Method method:methods) {System.out.println (modifier.tostring (Method.getmodifiers ()) /c9>+ "\ n" +//get the modification of a methodMethod.getreturntype (). Getsimplename () + "\ n" +//gets the type of the return valueMethod.getname ()//Get the name ); } System.out.println (Cls.getsimplename ()); }
Java Reflection mechanism