注解的初理解
注解
注解是java中给代码注释的,java中自带常用的注解有@Override是表重写父类方法的注解
元注解:java有四个元注解,有了这四个元注解我们就可以自己定义注解了
@Target /* 描述了注解修饰的对象范围,取值在java.lang.annotation.ElementType定义 METHOD:用于描述方法 PACKAGE:用于描述包 PARAMETER:用于描述方法变量 TYPE:用于描述类、接口或enum类型 */ @Retention /* 表示注解保留时间长短。取值在java.lang.annotation.RetentionPolicy定义 RetentionPolicy.SOURCE -------------注解将被编译器丢弃 RetentionPolicy.CLASS -------------注解在class文件中可用,但会被VM丢弃 RetentionPolicy.RUNTIME ---------VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息 RUNTIME > CLASS > SOURCE 自定义注解一般使用RUNTIME */ @Documented /* Documented注解的作用是:描述在使用 javadoc 工具为类生成帮助文档时是否要保留其注解信息。 */ @Inherited /* Inherited注解的作用是:使被它修饰的注解具有继承性(如果某个类使用了被@Inherited修饰的注解,则其子类将自动具有该注解) */
下面是自定义的注解:
//定义个自己的注解 @Target({ElementType.METHOD,ElementType.TYPE})//METHOD可以在方法上使用,TYPE可以在类上使用 @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation{ String name() default ""; //前面(String)是关键字,name()是变量名,default表示默认的值 int age() default 0; //如果定了的变量,没有给默认值,使用注解时需要传入值 int id() default 1; String[] schools() default {"重庆交通大学"}; //定义一个数组变量 }
反射获取注解:
public class Test02 { public static void main(String[] args) throws Exception { Class<?> aClass = Class.forName("TestDemo.annotation.Person"); System.out.println("获取全部注解:"); Annotation[] annotations = aClass.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } System.out.println("============================================="); System.out.println("获取指定类上的注解:"); MyType annotation = aClass.getAnnotation(MyType.class); System.out.println(annotation); System.out.println(annotation.value()); System.out.println("============================================="); System.out.println("获取指定属性上的注解"); Field name = aClass.getDeclaredField("name"); MyField myField = name.getAnnotation(MyField.class); System.out.println(myField); System.out.println(myField.columnName()); System.out.println(myField.type()); System.out.println(myField.length()); } } @MyType("sql_value") class Person{ @MyField(columnName = "sql_value",type = "int",length = 11) private int id; @MyField(columnName = "sql_value",type = "int",length = 11) private int age; @MyField(columnName = "sql_value",type = "String",length = 20) private String name; } @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface MyType{ String value(); } @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface MyField{ String columnName(); String type(); int length(); } /* 结果: 获取全部注解: @TestDemo.annotation.MyType(value=sql_value) ============================================= 获取指定类上的注解: @TestDemo.annotation.MyType(value=sql_value) sql_value ============================================= 获取指定属性上的注解 @TestDemo.annotation.MyField(columnName=sql_value, type=String, length=20) sql_value String 20 */
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)