public class Example_02 {
int i;
public float f;
protected boolean b;
private String s;
}
import java.lang.reflect.Field;
public class Main_02 {
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("类型为:"+field);
boolean isTurn = true;
while (isTurn){
try {
isTurn = 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,"MWQ");
}
System.out.println("修改后的值为:"+field.get(example));
}catch (Exception e){
System.out.println("在设置成员变量值时抛出的异常,下面执行setAccessible()方法");
field.setAccessible(true);
isTurn = true;
}
}
System.out.println();
}
}
}
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()方法");
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i