- package exercise;
- public class ReflectPoint {
- public int x=1;
- private int y= 2;
- String str1="aabbcc";
- String str2="aabbcc";
- String str3="aabbcc";
- public ReflectPoint(int x, int y, String str1, String str2, String str3) {
- super();
- this.x = x;
- this.y = y;
- this.str1 = str1;
- this.str2 = str2;
- this.str3 = str3;
- }
- @Override
- public String toString() {
- return "ReflectPoint [x=" + x + ", y=" + y + ", str1=" + str1
- + ", str2=" + str2 + ", str3=" + str3 + "]";
- }
-
- }
- <div class="blockcode"><blockquote>package exercise;
- import java.lang.reflect.Field;
- public class ReflectTest {
- public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
- ReflectPoint pt1=new ReflectPoint(0, 0, "oooiiiuuu", "pppoooiii", "oookkklll");
- Field[] fields = pt1.getClass().getFields();
- for(Field field : fields)
- {
- System.out.println(field.getType()==String.class);//为什么这里输出的结果是false呢,明明是字符串的
-
- if(field.getType()==String.class)
- {
- String valStr=(String)field.get(pt1);
- String newStr=valStr.replace('p', 'b');
- System.out.println(newStr);
- field.set(pt1,newStr);
-
- }
-
- }
-
-
-
-
-
-
- //System.out.println(pt1);
- }
- }
复制代码 |