- package day03;
- import java.lang.reflect.Field;
- public class Test {
- private int x;
- private String str;
- public float y;
- public static void main(String[] args) throws Exception{
- Test test = new Test();
- //获取 一个字节码对象
- Class cla = Class.forName("day03.Test");
- Field[] fields = cla.getDeclaredFields();
- for(Field field : fields){
- field.setAccessible(true);
- System.out.println(field.get(test));
- }
-
- }
- }
复制代码 我比较喜欢getDeclaredFields().这个方法,这个方法不管你什么权限都能拿到, |