Class can not access a member with modifiers "private"

通常遇到這個問題,一般多是在通過反射,調用方法的時候,遇到的

這是因爲反射調用方法,可以理解遇到private方法的時候,需要權限

所以,添加下面一句:method.setAccessible(true);

/**
      * @author: tianyong
      * @time: 2019/6/18 18:51
      * @description:反射調用方法,獲取return值
      */
    public static String reflectInvokeMethod(Class<?> clazz,String mn,Map params){
        String res = "";
        try {
            Method method = clazz.getMethod(mn,Map.class);
            method.setAccessible(true);         //添加訪問權限
            res = method.invoke(clazz.newInstance(),params).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;

如果你添加了這句話,仍舊不管用的話,那麼,可能因爲當前類中存在私有構造

我的就是又私有構造,找了半天,好傷森

如果本文幫到你了,歡迎關注本人微信公衆號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章