field
public class FieldBean {
private String name = "xq" ;
private static int versionCode = 100 ;
private final int height = 2 ;
private static final String KEY = "hello" ;
public int age = 18 ;
public static String versionName = "v1.0.0" ;
public final int width = 30 ;
public static final String TAG = FieldBean. class . getSimpleName ( ) ;
}
import java. lang. reflect. Field;
public class FieldTest {
public static void main ( String[ ] args) {
try {
Class< ? > bean = Class. forName ( "FieldBean" ) ;
Object instance = bean. newInstance ( ) ;
Field[ ] fields = bean. getFields ( ) ;
for ( int i = 0 ; i < fields. length; i++ ) {
System. out. println ( "" + fields[ i] ) ;
}
System. out. println ( "========================================" ) ;
Field field = bean. getDeclaredField ( "name" ) ;
field. setAccessible ( true ) ;
String name = ( String) field. get ( instance) ;
System. out. println ( "" + name) ;
System. out. println ( "========================================" ) ;
Field vCField = bean. getDeclaredField ( "versionCode" ) ;
vCField. setAccessible ( true ) ;
int versionCode1 = ( int ) vCField. get ( instance) ;
System. out. println ( "" + versionCode1) ;
vCField. set ( instance, 666 ) ;
int versionCode2 = ( int ) vCField. get ( instance) ;
System. out. println ( "" + versionCode2) ;
System. out. println ( "========================================" ) ;
Field height = bean. getDeclaredField ( "height" ) ;
height. setAccessible ( true ) ;
int height1 = ( int ) height. get ( instance) ;
System. out. println ( "" + height1) ;
System. out. println ( "========================================" ) ;
Field keyField = bean. getDeclaredField ( "KEY" ) ;
keyField. setAccessible ( true ) ;
String key1 = ( String) keyField. get ( instance) ;
System. out. println ( "" + key1) ;
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( NoSuchFieldException e) {
e. printStackTrace ( ) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
} catch ( InstantiationException e) {
e. printStackTrace ( ) ;
}
}
}
method
public class MethodBean {
public static void method1 ( ) {
System. out. println ( "============1无参数,无返回,public==============" ) ;
}
private static void method2 ( ) {
System. out. println ( "============2无参数,无返回,private==============" ) ;
}
public static void method3 ( String name, int age) {
System. out. println ( "============3带参数,无返回,public==============" +
name + " : " + age) ;
}
private static void method4 ( String name, int age) {
System. out. println ( "============4带参数,无返回,private==============" +
name + " : " + age) ;
}
public static String method5 ( String name, int age) {
System. out. println ( "============5带参数,带返回,public==============" +
name + " : " + age) ;
return name + " - " + age;
}
private static String method6 ( String name, int age) {
System. out. println ( "============6带参数,带返回,private==============" +
name + " : " + age) ;
return name + " - " + age;
}
}
import java. lang. reflect. InvocationTargetException;
import java. lang. reflect. Method;
public class MethodTest {
public static void main ( String[ ] args) {
test1 ( ) ;
test2 ( ) ;
test3 ( ) ;
test4 ( ) ;
}
private static void test4 ( ) {
System. out. println ( "=================test4=====================" ) ;
try {
Class< ? > bean = Class. forName ( "MethodBean" ) ;
Method[ ] methods = bean. getMethods ( ) ;
for ( int i = 0 ; i < methods. length; i++ ) {
System. out. println ( "" + methods[ i] . getName ( ) ) ;
}
System. out. println ( "===============================" ) ;
Method[ ] declaredMethods = bean. getDeclaredMethods ( ) ;
for ( int i = 0 ; i < declaredMethods. length; i++ ) {
System. out. println ( "" + declaredMethods[ i] . getName ( ) ) ;
}
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
}
}
private static void test3 ( ) {
System. out. println ( "=================test3=====================" ) ;
try {
Class< ? > bean = Class. forName ( "MethodBean" ) ;
{
Method method5 = bean. getMethod ( "method5" , String. class , int . class ) ;
Object o5 = bean. newInstance ( ) ;
Object invoke5 = method5. invoke ( o5, "xq" , 18 ) ;
System. out. println ( "method5===============" + invoke5) ;
Method method6 = bean. getDeclaredMethod ( "method6" , String. class , int . class ) ;
method6. setAccessible ( true ) ;
Object o6 = bean. newInstance ( ) ;
Object invoke6 = method6. invoke ( o6, "xq6" , 25 ) ;
System. out. println ( "method6===============" + invoke6) ;
}
} catch ( ClassNotFoundException
| NoSuchMethodException
| IllegalAccessException
| InstantiationException
| InvocationTargetException e) {
e. printStackTrace ( ) ;
}
}
private static void test2 ( ) {
System. out. println ( "=================test2=====================" ) ;
try {
Class< ? > bean = Class. forName ( "MethodBean" ) ;
{
Method method3 = bean. getMethod ( "method3" , String. class , int . class ) ;
Object o3 = bean. newInstance ( ) ;
Object invoke3 = method3. invoke ( o3, "xq" , 18 ) ;
System. out. println ( "method3===============" + invoke3) ;
Method method4 = bean. getDeclaredMethod ( "method4" , String. class , int . class ) ;
method4. setAccessible ( true ) ;
Object o4 = bean. newInstance ( ) ;
Object invoke4 = method4. invoke ( o4, "xq4" , 25 ) ;
System. out. println ( "method4===============" + invoke4) ;
}
} catch ( ClassNotFoundException
| NoSuchMethodException
| IllegalAccessException
| InstantiationException
| InvocationTargetException e) {
e. printStackTrace ( ) ;
}
}
private static void test1 ( ) {
System. out. println ( "=================test1=====================" ) ;
try {
Class< ? > bean = Class. forName ( "MethodBean" ) ;
Method method1 = bean. getMethod ( "method1" ) ;
Object o1 = bean. newInstance ( ) ;
Object invoke1 = method1. invoke ( o1) ;
System. out. println ( "method1===============" + invoke1) ;
Method method2 = bean. getDeclaredMethod ( "method2" ) ;
method2. setAccessible ( true ) ;
Object o2 = bean. newInstance ( ) ;
Object invoke2 = method2. invoke ( o2) ;
System. out. println ( "method2===============" + invoke2) ;
} catch ( ClassNotFoundException
| NoSuchMethodException
| IllegalAccessException
| InstantiationException
| InvocationTargetException e) {
e. printStackTrace ( ) ;
}
}
}