Java反射-Reflection
发布时间:
更新时间:
🕒 阅读时间:2 min read
👀 阅读量:Loading...
获取Class对象的三种方式
// 第一种方式 类名.classClass<User> userClass = User.class;// 第二种方式 对象.getClass();User user = new User();Class<?> userClass = user.getClass();// 第三种方式 Class.forName("类全路径"); 类的静态块会立马被执行Class<?> strClass = Class.forName("java.lang.String");获取类声明的字段
// 获得类声明的字段,获得public private的都可以,但是无法获得父类型的字段Field[] fileds = userClass.getDeclaredFields();// 只获得public字段,包括父类的public字段Field[] fields = userClass.getFields();// 若要获得父类的所有字段先获得父类对象然后获得父类的所有字段Field[] fieldsFu = userClass.getSuperFields().getDeclaredFields();方法概述
总的来说,带Declared的可获取到所有(包括父类)public,不带Declared的可获取到本类public的字段
getDeclaredFields(); getFields();getDeclaredField(); getField();getDeclaredMethods(); getMethods();getDecalreMethod(); getMethod();getDeclareConstructors(); getConstructors();getDeclareConstructor(); getConstructor();getDeclareAnnotations(); getDnnotations();getDeclareAnnotation(); getnnotation();Java反射-Reflection
本文链接: https://oxai.net.cn/posts/4a4883be
本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。
留言评论