Field类详解

更新时间:2023-08-08 18:36:21 阅读: 评论:0

新东方考研怎么样Field 类详解
me too 是什么意思
⼆、Field 类实例1. //AccessibleObject 类是 Field 、Method 和 Constructor 对象的基类。  2. public  class  AccessibleObject implements  AnnotatedElement {  3.    /*  4.      * 1、实现了AnnotatedElement 对注解⽀持的相关⽅法  5.      * 2、提供访问控制  6.      /  7.    void  tAccessible(boolean  flag)  8.    设置该对象(Field,Constructor,Method)是否可访问  9.    boolean  isAccessible()  10.    该对象是否可访问  11.    void  tAccessible(AccessibleObject[] array, boolean  flag)  12.    设置这⼀组对象(Field,Constructor,Method)是否可访问  13.  14. }  15. public  interface  Member{  16.    public  static  final  int  PUBLIC = 0; //标识类或接⼝的所有已声明成员的集合。  17.    public  static  final  int  DECLARED = 1; //标识类或接⼝的所有公共成员(包括继承成员)的集合。  18.      19.    public  Class<?> getDeclaringClass();// 所在类  20.    public  String getName(); //返回此 Member 表⽰的底层成员或构造⽅法的简单名称。  21.    public  int  getModifiers(); //作为整数返回由此 Member 所表⽰的成员或构造⽅法的 Java 语⾔修饰符。  22.    public  boolean  isSynthetic(); //如果此成员是编译器引⼊的,则返回 true ;否则,返回 fal 。  23.  24. }  25. public  final  class  Field extends  AccessibleObject implements  Member{  26.    27.    // 1.字段的设值和取值 ,对于静态属性,obj 传null  28.    t(Object obj, Object value)  29.    tXX(Object obj, Object value) ,⽐如tInt,tBoolean  30.    Object get(Object obj)  31.    getXX(Object obj) ,⽐如getInt,getBoolean  32.      33.      34.    // 2.字段上注解的
获取  35.    getDeclaredAnnotations()  36.    getAnnotation(Class<T> annotationClass);  37.      38.    // 3.字段类型  39.      Type getGenericType();  40.      Class<?> getType();  41.    // 4.字段修饰符  42.      int  Modifiers();  43.      String modify = String(modifiers);  44.    // 5.字段名称  45.      String getName();  46. }  [java]  1. package  reflect;  2.  3. import  java.lang.annotation.Annotation;  4. import  flect.Field;  5. import  flect.Modifier;  6. import  flect.ParameterizedType;  7. import  flect.Type;  8. import  flect.TypeVariable;  9. import  flect.WildcardType;  10. import  java.util.Arrays;  11. import  java.util.List;  12.  13. /** 14.  * @author zhangquanit  15.  */  16. public  class  FieldTest {  17.  18.    private  int  mInt; // 普通成员变量  19.  20.    public  static  String mStr; // 静态成员变量  21.
人教版七年级英语上册21.
22. @MyAnnotation
23. private Object mObject; // 使⽤了注解的成员变量
24.
25. private List<String> mList; // 泛型字段
26.
走遍美国视频下载27. public static void main(String[] args) throws Exception {
28.        FieldTest obj = new FieldTest();
29.        Class<? extends FieldTest> clazz = Class();
be的过去式30.
31. /*
32.          * 普通属性字段的设置和获取
33.          */
34.        Field normalField = DeclaredField("mInt");
35.        tAccessible(normalField);
36.        String Name();//mInt
汉语成为全球通用语言37.        normalField.t(obj, 100); // 设值
38. int mIntValue = Int(obj);// 取值 100;
39.
40. /*
41.          * 静态属性字段的设值和获取 (obj传null)
42.          */
43.        Field staticField = DeclaredField("mStr");
44.        tAccessible(staticField);
45.        staticField.t(null, "static value");
46.        Object value = (null);// static value
47.
48. /*
49.          * 字段的枚举相关操作 (更多相关信息参照我写的 Java注解)
50.          */
51.        Field annotationField = DeclaredField("mObject");
52.        tAccessible(annotationField);
53.        Annotation[] declaredAnnotations = annotationField
54.                .getDeclaredAnnotations();
55.        System.out.String(declaredAnnotations));
56.
57. /*
58.          * 泛型字段
59.          */
60.        Field genericField = DeclaredField("mList");
61.        tAccessible(genericField);
62.        Type genericType = GenericType();// java.util.List<java.lang.String>
63.        Class type = Type(); // interface java.util.List
64.        getActualType(genericType); //Class类型: class java.lang.String
65.
66.    }
67.
68. // 私有的变量,需要设置为可访问
69. private static void tAccessible(Field field) {
70. if (!field.isAccessible()) {985大学名单排名
71.            field.tAccessible(true);
72.        }泰坦尼克号英文
73.    }
加拿大留学中介排名
74.health是什么意思
75. // 获取List中泛型的实际类型
76. private static void getActualType(Type genericType) {
77. if (genericType instanceof ParameterizedType) {
78.            ParameterizedType parameterizedType = (ParameterizedType) genericType;
79.            Type actualType = ActualTypeArguments()[0];
80. if (actualType instanceof TypeVariable) {// 泛型类型,⽐如T
81.                TypeVariable typeVariable = (TypeVariable) actualType;
82.                System.out.println("TypeVariable类型: " + typeVariable);
83.
84.            } el if (actualType instanceof WildcardType) {// 含通配符? 类型
85.                WildcardType wildcardType = (WildcardType) actualType;
86.                System.out.println("WildcardType类型: " + wildcardType);
87.
88.            } el if (actualType instanceof Class) { // 普通类对象
89.                Class cls = (Class) actualType;
90.                System.out.println("Class类型: " + actualType); // class
91. // java.lang.String
92.            }
93.        }
94.    }
95.
96. }

本文发布于:2023-08-08 18:36:21,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/191741.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:成员   字段   排名   设置   变量   静态
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图