A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 丶小天 中级黑马   /  2014-2-20 11:41  /  1156 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.lang.reflect.*;
  2. public class ReflectTest {
  3.         public static void main(String[] args) throws Exception{
  4.                
  5.                 Book bk1 = new Book("java program","yellow","qing hua da xue chu ban she",345);
  6.                 Field[] fields = bk1.getClass().getFields();
  7.                 for(Field field:fields){
  8.                         if(field.getType() == String.class){
  9.                                 String oldValue = (String)field.get(bk1);
  10.                                 String newValue = oldValue.replace('a', 'l');
  11.                                 field.set(bk1,newValue);
  12.                         }
  13.                 }
  14.                 System.out.println(bk1);
  15.         }       
  16. }
  17. class Book {
  18.         String name;
  19.         String color ;
  20.         String chubanshe ;
  21.         int page;
  22.         Book(String name, String color, String chubanshe,int page) {
  23.                
  24.                 this.name = name;
  25.                 this.color = color;
  26.                 this.chubanshe = chubanshe;
  27.                 this.page = page;
  28.         }
  29.         public String toString() {
  30.                 return "Book:name=" + name + ",\r\ncolor=" + color + ", \r\nchubanshe="
  31.                                 + chubanshe + ",\r\npage="+page;
  32.         }
  33. }
复制代码

上面这段代码修改字符串没有成功  请高手解释一下为什么?

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

2 个回复

倒序浏览
  1. import java.lang.reflect.Field;

  2. public class My
  3. {
  4.         public static void main(String[] args) throws Exception
  5.         {

  6.                 Book bk1 = new Book("java program", "yellow",
  7.                                 "qing hua da xue chu ban she", 345);
  8.                 Field[] fields = bk1.getClass().getDeclaredFields();
  9.                 for (Field field : fields)
  10.                 {
  11.                         if (field.getType() == String.class)
  12.                         {
  13.                                
  14.                                 String oldValue = (String) field.get(bk1);
  15.                                 String newValue = oldValue.replace('a', 'l');
  16.                                 field.set(bk1, newValue);
  17.                         }
  18.                 }
  19.                 System.out.println(bk1);
  20.         }
  21. }

  22. class Book
  23. {
  24.         String        name;
  25.         String        color;
  26.         String        chubanshe;
  27.         int                page;

  28.         Book(String name, String color, String chubanshe, int page)
  29.         {

  30.                 this.name = name;
  31.                 this.color = color;
  32.                 this.chubanshe = chubanshe;
  33.                 this.page = page;
  34.         }

  35.         public String toString()
  36.         {
  37.                 return "Book:name=" + name + ",\r\ncolor=" + color + ", \r\nchubanshe="
  38.                                 + chubanshe + ",\r\npage=" + page;
  39.         }
  40. }
复制代码
Field[] fields = bk1.getClass().getDeclaredFields();  这个地方你用错方法了,  应该用这一个 。  多查api文档 ,
下面是不是你要的结果  .
Book:name=jlvl progrlm,
color=yellow,
chubanshe=qing hul dl xue chu bln she,
page=345

替换掉其中的a。

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
String        name;
String        color;
String        chubanshe;
int                page;
你这个没加修饰符,默认是default修饰类型的,而用getFileds()是取出public的属性getDeclaredFields()是取出非public类型的属性
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马