反射学习的记录

  1 ### 问题1 怎样通过类名获取反射对象?
  2 ```java
  3         String name = Person.class.getName();
  4         //获取类对象
  5         Class<?> clazz = Class.forName(name);
  6         //示例化对象
  7         Object o = clazz.newInstance();
  8 ```
  9 ### 问题2 怎样给反射对象赋值?
 10 ```java
 11    String name = Person.class.getName();
 12         //获取类对象
 13         Class<?> clazz = Class.forName(name);
 14         //示例化对象
 15         Object o = clazz.newInstance();
 16         Field field = o.getClass().getDeclaredField("age");
 17         field.setAccessible(true);
 18         //反射赋值
 19         field.set(o,2);
 20 ```
 21 ### 问题3 怎样调用反射方法?
 22 ```java
 23 {
 24         String name = Person.class.getName();
 25         //获取类对象
 26         Class<?> clazz = Class.forName(name);
 27         //示例化对象
 28         Object o = clazz.newInstance();
 29         Field field = o.getClass().getDeclaredField("age");
 30         Field fieldName = o.getClass().getDeclaredField("name");
 31         field.setAccessible(true);
 32         //反射赋值
 33         field.set(o,2);
 34         fieldName.set(o,"小王");
 35         System.out.println(o.toString());
 36         //反射调用指定方法
 37         Method[] methods = clazz.getMethods();
 38         //也可以获取到方法
 39         Method methodSetName = clazz.getMethod("setName",String.class);
 40         methodSetName.invoke(o,"小王真帅");
 41         for(Method method: methods)
 42         {
 43             String methodName = method.getName();
 44             //方法名称
 45             if("getName".equals(methodName))
 46             {
 47                 //调用方法 对象,传参
 48                 Object invoke = method.invoke(o, null);
 49             }
 50         }
 51         
 52         
 53 
 54     }
 55 ```
 56 ### 问题4 反射获取注解内容
 57 ```java
 58 //获取自定义注解 @Server 该类的所有注解
 59         System.out.println("--------class----------");
 60         if (clazz.isAnnotationPresent(Server.class))
 61         {
 62             Server annotation = clazz.getAnnotation(Server.class);
 63             String value = annotation.value();
 64             System.out.println(value);
 65         }
 66         //通过反射获取@Server注解的值
 67         Annotation[] annotations = clazz.getAnnotations();
 68         //获取到所有的注解
 69         for (Annotation ans : annotations)
 70         {
 71             Class<? extends Annotation> server = ans.getClass();
 72             Field[] fields = server.getFields();
 73             Method[] fms = server.getMethods();
 74             for (Method fm : fms)
 75             {
 76                 if ("value".equals(fm.getName()))
 77                 {
 78                     Object invoke = fm.invoke(ans, null);
 79                     System.out.println(invoke);
 80                 }
 81             }
 82         }
 83         System.out.println("--------Field----------");
 84         // 获取自定义@UserName @UserAge 定义在属性内
 85         Field[] declaredFields = clazz.getDeclaredFields();
 86         for (Field dfs : declaredFields)
 87         {
 88             if (dfs.isAnnotationPresent(UserName.class))
 89             {
 90                 UserName annotation = dfs.getAnnotation(UserName.class);
 91                 String name1 = annotation.name();
 92                 System.out.println("@UserName " + name1);
 93             }
 94             if (dfs.isAnnotationPresent(UserAge.class))
 95             {
 96                 UserAge annotation = dfs.getAnnotation(UserAge.class);
 97                 int age = annotation.age();
 98                 System.out.println("@UserAge " + age);
 99             }
100         }
101         System.out.println("--------Method----------");
102         Method[] declaredMethods = clazz.getDeclaredMethods();
103         for (Method ds : declaredMethods)
104         {
105             if (ds.isAnnotationPresent(UserMethod.class))
106             {
107                 UserMethod annotation = ds.getAnnotation(UserMethod.class);
108                 String value = annotation.value();
109                 System.out.println("@Method "+ value);
110             }
111         }
112 ```
113 
114 ### 5.反射获取注解内容然后赋值给
115 ```java
116  //获取到注解赋值
117         System.out.println("-------注解赋值------");
118         Field[] declareds = clazz.getDeclaredFields();
119         for (Field dfs : declareds)
120         {
121             if (dfs.isAnnotationPresent(UserName.class))
122             {
123                 UserName annotation = dfs.getAnnotation(UserName.class);
124                 String name1 = annotation.name();
125                 System.out.println("@UserName " + name1);
126                 dfs.setAccessible(true);
127                 dfs.set(o,name1);
128             }
129             if (dfs.isAnnotationPresent(UserAge.class))
130             {
131                 UserAge annotation = dfs.getAnnotation(UserAge.class);
132                 int age = annotation.age();
133                 System.out.println("@UserAge " + age);
134                 dfs.setAccessible(true);
135                 dfs.set(o,age);
136             }
137         }
138         System.out.println(o.toString());
139 ```

 

posted @ 2022-01-11 21:49  奇十三  阅读(28)  评论(0)    收藏  举报