- package test;
- class ReflectPoint {
-
- public String str1="ball";
- public String str2="baskball";
- public String str3="itheim";
- @Override
- public String toString() {
- return "ReflectPoint [str1=" + str1 + ", str2=" + str2 + ", str3=" + str3
- + "]";
- }
- }
复制代码- public class ReflectTest {
- public static void main(String[] args) throws Exception{
- // TODO Auto-generated method stub
-
- ReflectPoint rp=new ReflectPoint();
- changeVlaue(rp);
- System.out.println(rp);
- }
- private static void changeVlaue(Object obj)throws Exception {
- // TODO Auto-generated method stub
- Field [] fields=obj.getClass().getFields();
- for (Field field : fields) {
- if(field.getType()==String.class){
- String oldValue=(String)field.get(obj);
- String newValue=oldValue.replace('b', 'a');
-
- field.set(obj, newValue);
- }
-
- }
- }
-
- }
复制代码 请问各位,这个程序里面的ReflectPoint类中的成员为何一定要加public,不然不能将里面的b替换成a!谢谢!
|
|