我是搬运工

博客园 首页 联系 订阅 管理
public class Calculator2{
 
     private int add(int a,int b){
          
         return a+b;
     }
     
}
public class Test {
    public static void main(String[] args){
             Calculator2 calculator2 = new Calculator2();
             Class<Calculator2> clazz = Calculator2.class;
             Object result = null;
             try{
             Method method = clazz.getDeclaredMethod("add", new Class[]{Integer.TYPE,Integer.TYPE});
             method.setAccessible(true);
             result =  method.invoke(calculator2, new Object[]{2,3});            
             }catch (Exception e) {
             e.printStackTrace();
            }finally{              
              System.out.println(result); 
              }        
    }
}

 

posted on 2016-01-25 15:37  我是搬运工  阅读(255)  评论(0编辑  收藏  举报