例题
创建一个Example_02类,一次声明int、float、boolean、String型得成员变量,并将他们设置为不同得访问权限。然后通过反射访问Example_02类中的所有成员变量,将成员变量得名称和类型信息输出到控制台,并分别将各个成员变量修改前后得值输出到控制台。
public class Example_02 {
int i;
public float f;
protected boolean b;
private String s;
}
import java.lang.reflect.Field;
public class Example_02min {
public static void main(String[] args) {
Example_02 example = new Example_02();
Class exampleC = example.getClass();
Field[] declaredFields = exampleC.getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
Field field = declaredFields[i];
System.out.println("名称为:" + field.getName());
Class fieldType = field.getType();
System.out.println("类型为:" + fieldType);
boolean isTrue = true;
while (isTrue) {
try {
isTrue = false;
System.out.println("修改前的值:" + field.get(example));
if (fieldType.equals(int.class)) {
System.out.println("利用方法setInt()修改成员变量的值");
field.setInt(example, 168);
} else if (fieldType.equals(float.class)) {
System.out.println("利用方法setFloat()修改成员变量的值");
field.setFloat(example, 99.9F);
} else if (fieldType.equals(boolean.class)) {
System.out.println("利用方法setBoolean()修改成员变量的值");
field.setBoolean(example, true);
} else {
System.out.println("利用方法set()修改成员变量的值");
field.set(example, "ZQChandson"); //可以为各种类型的成员变量赋值
}
System.out.println("修改后的值:" + field.get(example));
} catch (Exception e) {
//String s;权限为private,需要先设置setAccessible()再修改
System.out.println("在设置成员变量时抛出异常,下面执行setAccessible()方法");
field.setAccessible(true);
isTrue = true;
}
}
System.out.println("---------------------------");
}
Field field = declaredFields[3];
try {
field.set(example, "hi");
System.out.println(field.get(example));
field.setAccessible(false); //修改过一次后就一直是true了
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
例题
创建一个Example_03类,并编写四个典型得方法,然后通过反射访问,将各个方法得名称、入口参数类型、返回值类型输出。
public class Example_03 {
static void staticMethod() {
System.out.println("执行staticMethod()方法");
}
public int publicMethod(int i) {
System.out.println("执行publicMethod()方法");
return i * 100;
}
protected int protectedMethod(String s, int i) throws NumberFormatException {
System.out.println("执行protectedMethod()方法");
return Integer.valueOf(s) + i;
}
private String privateMethod(String... strings) {
System.out.println("执行privateMethod()方法");
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < strings.length; i++) {
stringBuffer.append(strings[i]);
}
return stringBuffer.toString();
}
}
import java.lang.reflect.Method;
public class Example_03min {
public static void main(String[] args) {
Example_03 example = new Example_03();
Class exampleC = example.getClass();
Method[] declaredMethods = exampleC.getDeclaredMethods();
for (int i = 0; i < declaredMethods.length; i++) {
Method method = declaredMethods[i];
System.out.println("名称为:" + method.getName());
System.out.println("是否允许带有可变数量的参数:" + method.isVarArgs());
System.out.println("入口参数类型依次为:");
Class[] parameterTypes = method.getParameterTypes();
for (int j = 0; j < parameterTypes.length; j++) {
System.out.println(" " + parameterTypes[j]);
}
System.out.println("返回值类型:" + method.getReturnType());
System.out.println("可能抛出的异常:");
Class[] exceptionTypes = method.getExceptionTypes();
for (int j = 0; j < exceptionTypes.length; j++) {
System.out.println(" " + exceptionTypes[j]);
}
boolean isTrue = true;
while (isTrue) {
try {
isTrue = false;
if ("staticMethod".equals(method.getName())) {
method.invoke(example);
} else if ("publicMethod".equals(method.getName())) {
System.out.println("返回值为:"
+ method.invoke(example, 168));
} else if ("protectedMethod".equals(method.getName())) {
System.out.println("返回值为:"
+ method.invoke(example, "7", 5));
} else if ("privateMethod".equals(method.getName())) {
Object[] parameters = new Object[]{new String[]{"M", "W", "Q"}};
System.out.println("返回值为:"
+ method.invoke(example, parameters));
}
} catch (Exception e) {
System.out.println("在执行方法时出现异常,执行下面的setAccessible()方法");
method.setAccessible(true);
isTrue = true;
}
}
System.out.println();
}
}
}