java自定义注解学习(二)_注解详解
上篇文章,我们简单的实现了一个自定义注解,相信大家对自定义注解有了个简单的认识,这篇,这样介绍下注解中的元注解和内置注解
整体图示
内置注解
@Override 重写覆盖
这个注解大家应该经常用到,主要在子类重写父类的方法,比如toString()
方法
package com.kevin.demo; public class Demo1 { @Override public String toString(){ return "demo1"; } }
@Deprecated 过时
@Deprecated
可以修饰的范围很广,包括类、方法、字段、参数等,它表示对应的代码已经过时了,程序员不应该使用它,不过,它是一种警告,而不是强制性的。
package com.kevin.demo; public class Demo1 { @Deprecated public void goHome(){ System.out.println("过时的方法"); } }
idea中调用这些方法,编译器也会显示删除线并警告
@SuppressWarning 压制Java的编译警告
@SuppressWarnings
表示压制Java的编译警告,它有一个必填参数,表示压制哪种类型的警告.
| 关键字| 用途|
| -------- | :----- |
| all | to suppress all warnings
|boxing | to suppress warnings relative to boxing/unboxing operations
| cast | to suppress warnings relative to cast operations
| dep-ann | to suppress warnings relative to deprecated annotation
| deprecation | to suppress warnings relative to deprecation
| fallthrough | to suppress warnings relative to missing breaks in switch statements
| finally | to suppress warnings relative to finally block that don¡¯t return
| hiding | to suppress warnings relative to locals that hide variable
| incomplete-switch | to suppress warnings relative to missing entries in a switch statement (enum case)
|nls | to suppress warnings relative to non-nls string literals
|null | to suppress warnings relative to null analysis
| rawtypes | to suppress warnings relative to un-specific types when using generics on class params
| restriction | to suppress warnings relative to usage of discouraged or forbidden references
| serial | to suppress warnings relative to missing serialVersionUID field for a serializable class
|static-access | to suppress warnings relative to incorrect static access
| synthetic-access | to suppress warnings relative to unoptimized access from inner classes
| unchecked | to suppress warnings relative to unchecked operations
| unqualified-field-access | to suppress warnings relative to field access unqualified
| unused | to suppress warnings relative to unused code
上面的方法,我们就可以增加
@SuppressWarnings("deprecation") public static void main(String[] args) { Demo1 demo1 = new Demo1(); demo1.goHome(); }
元注解
元注解:注解的注解,即java为注解开发特准备的注解。
我们以上面讲到的java内置注解@Override为例,学习下java元注解
@Target(ElementType.METHOD) @Retention(RetentionPolicy.SOURCE) public @interface Override { }
@Target
@Target
表示注解的目标,@Override
的目标是方法(ElementType.METHOD
),ElementType
是一个枚举,其他可选值有:
- TYPE:表示类、接口(包括注解),或者枚举声明
- FIELD:字段,包括枚举常量
- METHOD:方法
- PARAMETER:方法中的参数
- CONSTRUCTOR:构造方法
- LOCAL_VARIABLE:本地变量
- ANNOTATION_TYPE:注解类型
- PACKAGE:包
目标可以有多个,用{}表示,比如@SuppressWarnings
的@Target
就有多个,定义为:
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.SOURCE) public @interface SuppressWarnings { String[] value(); }
如果没有声明@Target
,默认为适用于所有类型。我们上篇文章的demo就没有声明@Target
@Retention
@Retention
表示注解信息保留到什么时候,取值只能有一个,类型为RetentionPolicy
,它是一个枚举,有三个取值:
-
SOURCE
:只在源代码中保留,编译器将代码编译为字节码文件后就会丢掉 -
CLASS
:保留到字节码文件中,但Java虚拟机将class文件加载到内存时不一定会在内存中保留 -
RUNTIME
:一直保留到运行时
如果没有声明@Retention
,默认为CLASS
。
@Override
和@SuppressWarnings
都是给编译器用的,所以@Retention
都是RetentionPolicy.SOURCE
。
@Documented
用于指定javadoc生成API文档时显示该注解信息。Documented
是一个标记注解,没有成员。
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Documented { }
@Inherited
@Inherited
元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。
看个栗子
public class Demo1 { @Inherited @Retention(RetentionPolicy.RUNTIME) static @interface Test { } @Test static class Base { } static class Child extends Base { } public static void main(String[] args) { System.out.println(Child.class.isAnnotationPresent(Test.class)); } }
main
方法检查Child
类是否有Test注解,输出为true
,这是因为Test
有注解@Inherited
,如果去掉,输出就变成false
了
总结
好了,这篇先学习到这,我要好好看看这些知识,下篇介绍注解的解析啦。好了。玩的开心!
参考

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
java自定义注解学习(一)_demo小练习
自定义注解 现在大家开发过程中,经常会用到注解。 比如@Controller 等等,但是有时候也会碰到自定义注解,在开发中公司的记录日志就用到了自定义注解。身为渣渣猿还是有必要学习下自定义注解的。 这篇我们先写一个简单的注解列子,不会立马介绍各种什么元注解。从例子中感受下注解的作用 定义个注解 package com.kevin.annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface Kevin { String name() default "kevin"; } 解析并测试这个注解 package com.kevin; import com.kevin.annotation.Kevin; @Kevin public class Test { public static void showKevin(Class c) { System.out.p...
- 下一篇
java自定义注解学习(三)_注解解析及应用
上篇文章已经介绍了注解的基本构成信息。这篇文章,主要介绍注解的解析。毕竟你只声明了注解,是没有用的。需要进行解析。主要就是利用反射机制在运行时进行查看和利用这些信息 常用方法汇总 在Class、Field、Method、Constructor中都有如下方法: //获取所有的注解 public Annotation[] getAnnotations() //获取所有本元素上直接声明的注解,忽略inherited来的 public Annotation[] getDeclaredAnnotations() //获取指定类型的注解,没有返回null public <A extends Annotation> A getAnnotation(Class<A> annotationClass) //判断是否有指定类型的注解 public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) Annotation 是一个借口,它表示注解,源码为: public inter...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Red5直播服务器,属于Java语言的直播服务器
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS7设置SWAP分区,小内存服务器的救世主
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2全家桶,快速入门学习开发网站教程
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS7安装Docker,走上虚拟化容器引擎之路
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装