使用反射暴力訪問私有屬性

try {
Class clazz = Class.forName(“package.ClassName”);
Object instance = clazz.newInstance();
Field field = clazz.getDeclaredField(“privateFiled”);
field.setAccessible(true);
field.set(instance, true);
Object filed= field.get(instance);
// filed即爲獲取到的私有屬性
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}

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