为什么我后面得访问方法输出不了????加粗那段
package com.imooc;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Main_02 {
public static void main(String[] args) {
Example_02 ex = new Example_02();
Class c = ex.getClass();
Field[] fd = c.getDeclaredFields();
for(int i=0;i<fd.length;i++){
Field field = fd[i]; //历遍成员变量
//获得成员名称
System.out.println("成员名称:"+field.getName());
Class fieldType = field.getType(); //获得成员变量类型
System.out.println("成员变量类型:"+fieldType);
boolean isTurn = true;
while(isTurn){
try {
isTurn = false;
System.out.println("修改值前为:"+field.get(ex));
if(fieldType.equals(int.class)){
System.out.println("利用方法setint()修改成员变量的值");
field.setInt(ex, 168);
}else if(fieldType.equals(float.class)){
System.out.println("利用setFloat()修改成员变量得值");
field.setFloat(ex, 99.9F);
}else if(fieldType.equals(boolean.class)){
System.out.println("利用setBoolean()修改成员变量得值");
field.setBoolean(ex,true);
}else if(field.equals(String.class)){
System.out.println("用set()方法修改成员变量得值");
field.set(ex,"MWQ");
}else{
System.out.println("用set()方法修改成员变量得值");
field.set(ex,"RSF");
}
System.out.println("修改值后得成员变量"+field.get(ex));
}
catch (Exception e) {
System.out.println("在设置成员变量时抛出异常,"+"下面执行setAccessible()方法");
field.setAccessible(true);
isTurn = true;
}
}
System.out.println();
}
Method[] dm = c.getDeclaredMethods();
for(int i=0;i<dm.length;i++){
Method m = dm[i];
System.out.println("名称为:"+m.getName());
System.out.println("是否允许带有可变数量得参数:"+m.isVarArgs());
System.out.println("入口参数类型为:");
Class[] pt = m.getParameterTypes();
for(int j=0;j<pt.length;j++){
System.out.println(""+pt[j]);
}
System.out.println("返回值类型为:"+m.getReturnType());
System.out.println("可能抛出得异常类型有:");
Class[] et = m.getExceptionTypes();
for(int j=0;j<et.length;j++){
System.out.println(""+et[j]);
}
boolean isTurn = true;
while(isTurn){
try{
isTurn = false;
if("staticMethod".equals(m.getName()))
m.invoke(ex);
else if("publicMethod".equals(m.getName()))
System.out.println("返回值为:"+m.invoke(ex, 168));
else if("privateMethod".equals(m.getName()))
System.out.println("返回值为:"+m.invoke(ex,"5",7));
else if("privateMethod".equals(m.getName())){
Object[] o = new Object[]{new String[]{"Q","W","E"}};
//上面定义二维数组
System.out.println("返回类型:"+m.invoke(ex, o));
}
}catch (Exception e) {
System.out.println("在执行方法时可能抛出异常,所以下面执行setAccessible()方法");
m.setAccessible(true); //设置允许访问
isTurn = true;
}
}
System.out.println();
}
}
}