反射和注解
来源:互联网 发布:生死狙击淘宝租号 编辑:程序博客网 时间:2024/04/27 01:12
注解和反射
一. 反射
public class Demo01 {public static void main(String[] args) {//类---〉对象Person person=new Person("Wan",11);Class<?> clazz=person.getClass();System.out.println(clazz.getName()); //com.demo.Persontry {Class<?> clazz1=Class.forName("com.demo.Person");Person person2=(Person) clazz1.newInstance();System.out.println(person2);//反射 可以获取类中所有方法属性Constructor [] constructors=clazz1.getDeclaredConstructors();for (Constructor constructor : constructors) {System.out.println(constructor);}System.out.println("==获取属性==");Field[] fields=clazz1.getDeclaredFields();for (Field field : fields) {System.out.println(field);if(field.getName().equals("age")){field.setAccessible(true);field.set(person2, 7);}}System.out.println("person2 age"+person2.getAge());System.out.println("==获取方法==");Method[] methods =clazz1.getDeclaredMethods();for (Method method : methods) {System.out.println(method.getName());if(method.getName().equals("play")){method.setAccessible(true);method.invoke(person2, "123");}}} catch (Exception e) {e.printStackTrace();}}}
1.1 概念
正常情况下如果已经有一个类,则肯定可以通过类创建对象;那么如果现在要求通过一个对象找到一个类的名称,此时就需要用到反射机制
实例化对象-----〉getClass()方法------〉得到完整的“包.类”名称
取得类的结构
Constructor
表示类中的构造方法
Field
表示类中的属性
Method
表示类中的方法
二.注解
public class Demo01 {//注解 reflect 无注解 不反射 //@Override public static void main(String[] args) {Student student = new Student();Class<?> clazz = student.getClass();Method[] methods = clazz.getDeclaredMethods();for(Method method:methods){//判断method 是否带有注解boolean b = method.isAnnotationPresent(MyAnnotation.class);MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);if(b){//如果有注解 通过注解方式赋zhiSystem.out.println(method.getName());if(method.getName().equals("setName")){try {method.invoke(student,myAnnotation.name());} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(method.getName().equals("setAge")){ try {method.invoke(student,myAnnotation.age());} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}System.out.println(student.getAge());System.out.println(student.getName());}}
2.1 Annotation
可以用来修示类,属性,方法Annotation不影响程序运行,无论是否使用Annotation代码都可以正常执行
2.2 系统内建的Annotation
@Override
覆写的Annotation
@Deprecated
不赞成使用的Annotation
@SuppressWarnings
压制安全警告的Annotation
2.3 自定义的Annotation
[public] @interface Annotation 名称{ 数据类型 变量名称();}
阅读全文
0 0
- java反射和注解
- 注解和反射
- java反射和注解
- 反射和注解
- android 注解和反射
- Java注解和反射
- 反射和注解
- 反射和注解
- Java自定义注解和反射获取注解
- 黑马程序员-注解和反射
- 自定义注解和反射注释
- 自定义注解和反射实例
- Java中的反射和注解
- java中的反射和注解
- 反射和注解的使用
- 好玩的注解和反射
- java的反射和注解
- Java反射和注解笔记
- 查API,学新内容--- (零) 随机数
- 20170717-20170723C#工作学习周总结
- oracle提示15241TNS:无监听程序解决
- 方法类与匿名内部类,枚举类
- sql语句中left join、right join 以及inner join之间的使用与区别
- 反射和注解
- JS中的关键词
- JavaScript中的关键字
- label和input对齐的方法
- Qt5.7以上调用虚拟键盘(支持中文),以及源码修改(可拖动,水平缩放)
- Sereja ans Anagrams
- bzoj3522 [Poi2014]Hotel dfs(DP)
- U盘容量变小解决办法
- Redis的哨兵机制 Sentinel(简要)