- public static void main(String[] args) throws Exception {
- Student s =new Student(12,"zhangsan");
- Field[] fields=s.getClass().getDeclaredFields();
- System.out.println(fields.length);
- for(Field field :fields){
- System.out.println(field.getType());
- if(field.getType()==String.class){
- field.setAccessible(true);
- String oldValue =(String) field.get(s);
- System.out.println(oldValue);
- String newValue=oldValue.replace('b' ,'a');
- //field.set(oldValue, newValue);//这个写错了呵呵
- field.set(s,newValue);//传入对象,传入新值
- }
- }
- System.out.println(s);
复制代码 呵呵,刚才调试来着,你set方法错了,楼上正解
|