protected Reflect1(String name) {
System. out .println(name);
}
public void f() {
System. out .println( "invoke f" );
}
String mf( int j, Object... args) {
System. out .println( "invoke mf" );
return String. valueOf (j + args. length );
}
}
public class Rf {
public static void main(String[] args) throws Exception {
Class<Reflect1> clazz = Reflect1. class ;
Reflect1 rf1 = clazz.newInstance();
// 含有 Declared 字符串的是获取所有的元素,否则就只能获取公有元素
Field[] f = clazz.getDeclaredFields();
for (Field field : f) {
// 设置这里本不具有访问权限的元素为可访问
field.setAccessible( true );
// 使用基本数据类型专有的 API
if (field.getType().getCanonicalName().equals( "int" )
|| field.getType().getCanonicalName().equals(
"java.lang.Integer" )) {
field.setInt(rf1, 9);
}
System. out .println( "Field is " + field.getName() + "\t"
+ field.toGenericString() + "\t" + field.get(rf1));
}
}
}
控制台输出如下语句:
default
Field is i private int net.ilkj.reflect.Reflect1.i 9
Field is s protected java.lang.String net.ilkj.reflect.Reflect1.s null
Field is list
public java.util.List<java.lang.String> net.ilkj.reflect.Reflect1.list []
______________________________________________
(2.)Method :这个类用于获取类中的方法的信息以及访问这些方法的能力。
l getDefaultValue() :返回 此 方法 表示 的注 视 成员的默认值 ;