参考
https://zhuanlan.zhihu.com/p/73710795
概述
- 想照镜子一样,通过反射认识自己
- 每个类都有其 class 对象,class 对象中有类的各种信息
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| obj.getClass();
obj.getClass().getName();
Field[] fields = obj.getClass().getDeclaredFields();
field.getName();
field.setAccessible(true); field.get(obj);
Method[] methods = obj.getClass().getDeclaredMethods();
method.invoke(obj, args)
field.getType().getName()
private static Job getClass(String classname) throws Exception { Class<?> class1 = Class.forName(classname); return (Job) class1.newInstance(); }
|