本帖最后由 杨杨 于 2013-3-2 11:54 编辑
- package cn.itcast.day1;
- public class ReflectPoint {
- int x;
- int y;
- String str1="ab";
- String str2="abc";
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public String getStr1() {
- return str1;
- }
- public void setStr1(String str1) {
- this.str1 = str1;
- }
- public String getStr2() {
- return str2;
- }
- public void setStr2(String str2) {
- this.str2 = str2;
- }
- @Override
- public String toString(){
- return str1+str2;
- }
- }
复制代码- package cn.itcast.day1;
- import java.lang.reflect.Field;
- public class ReflectTest {
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- ReflectPoint point1= new ReflectPoint();
- ChangerVale(point1);
- System.out.println(point1);
- }
- private static void ChangerVale(Object obj) throws Exception {
- Field[] fields= obj.getClass().getFields();
- for(Field field:fields){
- if(field.getType()==String.class){
- String oldchar= (String)field.get(obj);
- String newString =oldchar.replace('b', 'a');// "a" "b" 与 'a' 'b'区别
- field.set(obj, newString);
- }
-
- }
- }
- }
复制代码 按照张老师讲的应该是输出aaaac
但输出的是 ababc
|