todayx.org
          todayx.org
          posts - 39,comments - 60,trackbacks - 0

          package me.test;
          import java.lang.reflect.*;  //導入反射需要的包
          public class ReflectTest {
           public static void main(String[] args)  throws  Exception
           { 
            
            /*  下面通過反射完成對一個對象中成員的替換
             *   并且執行執行私有方法 
             *   完成對 Poiont類的對象中所有的 String的對象的d換成x
             *   并且類中無修改方法   
             */
              Point pt=new Point(3,5);  //創建一個Point對象
              Field fx=pt.getClass().getField("x") ;  //獲取x的映射類對象
              Field fy=pt.getClass().getDeclaredField("y");//因為y是私有的所以要調用這個方法
              Method m2=Point.class.getDeclaredMethod("showPrivate") ;//獲得私有方法映射類 
             
              //利用反射調用共有輸出
              m2.setAccessible(true)  ;// 修改showPrivate 權限 改變為可以調用
                 m2.invoke(pt) ;//執行私有方法
              //利用成員反射輸出x 和 私有的 y 
              System.out.println(fx.getInt(pt));//反射輸出x 
              fy.setAccessible(true) ;//改變私有為可訪問
              System.out.println(fy.getInt(pt));//輸出私有y
                 
              //替換成員后并且反射私有方法輸出
              changeString(pt) ;//反射替換成員值
               System.out.println(pt);
             
             
           }
           public static void changeString(Object obj)  throws Exception//反射替換對所有String進行替換
           {
            Field[] f=obj.getClass().getFields()  ; //獲得成員映射數組
            for(Field tem : f)  //迭代for循環
            {
             if(tem.getType()==String.class)  //內存中只有一份String字節碼
             {
              String oldString=(String)tem.get(obj) ;  //返回內容
              String newString=oldString.replace('d', 'x');//將所有b替換為x 
              tem.setAccessible(true);
               tem.set(obj, newString) ;//替換成員值
             }
             
            }
            
           }

          }


          public class Point
          {
           public int x  ;
           private int y  ;
           public Point(int x, int y) {
            super();
            this.x = x;
            this.y = y;
           }
             public String  a="dsfdsfd"  ;  //只有 共有可以替換
             public String  b="fdsfdsfewewwwww"  ;
             public String  c="adddssss"  ;

           private void showPrivate()  //私有方法輸出
           {
            System.out.println("x="+this.x+"\n"+"y="+this.y);
            System.out.println(this.a);
            System.out.println(this.b);
            System.out.println(this.c);
           }

           public String toString()
           {
            return this.a+"\n"+this.b+"\n"+this.c;
           }

          }


          歷史上的今天
          回顧歷史的今天,歷史就像生活的一面鏡子;可以了解歷史的這一天發生的事件;借古可以鑒今;歷史是不能忘記的.要記住歷史的每一天
          http://www.todayx.org/
          posted on 2012-01-15 10:49 todayx.org 閱讀(1617) 評論(1)  編輯  收藏

          FeedBack:
          # re: Java反射高級應用--利用反射調用類的私有方法修改私有方法值,以及替換Java的類成員數據
          2012-01-16 12:13 | 旺仔
          我勒個去,這個就算是高級應用了?  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 乌苏市| 古丈县| 蕉岭县| 左云县| 讷河市| 军事| 沐川县| 荔波县| 岢岚县| 桐梓县| 渭南市| 丘北县| 台中县| 呼伦贝尔市| 德昌县| 石阡县| 襄樊市| 泸州市| 大渡口区| 石嘴山市| 高尔夫| 西城区| 新龙县| 如东县| 泽州县| 广州市| 无为县| 永和县| 青海省| 随州市| 霍邱县| 长寿区| 乐都县| 马鞍山市| 柏乡县| 周口市| 凤翔县| 英吉沙县| 荥阳市| 蕉岭县| 桐梓县|