DANCE WITH JAVA

          開發出高質量的系統

          常用鏈接

          統計

          積分與排名

          好友之家

          最新評論

          調用java的私有方法

          package base;
          ?
          import java.lang.reflect.Method;
          public class PrivateTestCall {
          ?
          ?/**
          ? * @param args
          ? */
          ?public static void main(String[] args) {
          ??// TODO Auto-generated method stub
          ??try{
          ???//method one
          ???Method m=PrivateTest.class.getDeclaredMethod("print", new Class[]{});
          ???m.setAccessible(true);
          ???m.invoke(new PrivateTest(), new Object[]{});
          ???//method two
          ???PrivateTest privateTest=new PrivateTest();
          ???Method m1=privateTest.getClass().getMethod("print2", null);
          ???m1.setAccessible(true);
          ???m1.invoke(privateTest, null);
          ???//method three
          ???Method m2=PrivateTest.class.getMethod("print2", null);
          ???m2.setAccessible(true);
          ???m2.invoke(new PrivateTest(), null);
          ??}catch(Exception e){
          ???e.printStackTrace();
          ??}
          ?}
          ?
          }

          ?

          package base;
          ?
          public class PrivateTest {
          ?private void print(){
          ??System.out.println("in private method");
          ?}
          ?public void print2(){
          ??System.out.println("in public method");
          ?}
          }

          posted on 2006-09-20 21:18 dreamstone 閱讀(753) 評論(0)  編輯  收藏 所屬分類: jdk相關

          主站蜘蛛池模板: 福贡县| 景洪市| 宝鸡市| 平安县| 玉田县| 宜兴市| 嘉定区| 偏关县| 明水县| 故城县| 同德县| 利津县| 永城市| 木里| 鲁甸县| 鹿邑县| 银川市| 白山市| 潮安县| 西丰县| 高唐县| 杭锦后旗| 龙门县| 武清区| 青岛市| 田林县| 应用必备| 南雄市| 精河县| 繁峙县| 沭阳县| 湖州市| 克山县| 杭州市| 灵璧县| 镇赉县| 石家庄市| 萨嘎县| 建始县| 原平市| 新竹县|