java教程之java注解annotation使用方法
发布时间:2023-11-13 20:18:57
1.概述
注解可以定义到方法上,类上,一个注解相当与一个类,就相当于实例了一个对象,加上了注解,就相当于加了一个标志。
常用的注解:
@Override:表示重新父类的方法,
这个也可以判断是否覆盖的父类方法,在方法前面加上此语句,如果提示的错误,那么你不是覆盖的父类的方法,要是提示的没有错误,那么就是覆盖的父类的方法。
@SuppressWarnings("deprecation"):取消编译器的警告(例如你使用的方法过时了)
@Deprecated:在方法的最上边也上此语句,表示此方法过时,了,或者使用在类上面
import java.util.ArrayList;
import java.util.List;
public class annotationDemo {
/*
* 对于集合,如果没有指定存储的类型,那么就会有安全警告,
* 如果不想提示安全警告的话,那么就所在类或者方法上添加@SuppressWarnings(参数)
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List list=new ArrayList();
}
}
2.自定义注解
1.格式
权限 @interface 注解名称 { }
步骤:
定义注解类--->定义应用注解类的类--->对应用注解类的类进行反射的类(这个类可以另外定义,也可以是在应用注解类中进行测试)
import java.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
//定义此注解保留在字节码中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
@MyAnnotation
// 应用定义的注解类
public class ApplyMyAnnotation {
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}
2.声明周期
格式:例如:@Retention(RetentionPolicy.CLASS)
在自定一的注解类上定义周期,@Retention(参数类型) 参数类型是RetentionPolicy
RetentionPolicy.CLASS:类文件上,运行时虚拟机不保留注解
RetentionPolicy.RUNTIME:类文件上,运行时虚拟就保留注解
RetentionPolicy.SOURCE:源文件上,丢弃注解
SuppressWarnings和Override是RetentionPolicy.SOURCE,
Deprecated是在RetentionPolicy.RUNTIME,要向运行时调用定义的一样,那么必须是RetentionPolicy.RUNTIME,
默认的都是RetentionPolicy.CLASS:
3.指定目标
格式:例如:方法上@Target(ElementType.METHOD)
定义的注解可以注解什么成员。如果不声明此注解,那么就是可以放到任何程序的元素上。
可以是包,接口,参数,方法,局部变量,字段…等。
//定义此注解保留在字节码中
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})//可以定义在方法上和类上接口,表示类型
public @interface MyAnnotation {
}
@MyAnnotation
// 应用定义的注解类
public class ApplyMyAnnotation {
@MyAnnotation//定义在方法上
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}
3.为注解添加属性
1.类型
注解的属性置可以是:8个基本数据类型,String,枚举,注解,Class,数组类型,
2.注意点
当注 解中只有一个属性或者是只有一个属性需要赋值的话,那么在调用的时候,就可以直接写入,不需要指定属性名,
当注解的属性是数组类型并且赋值的时候只赋值一个值,那么就可以省略{}.
3.示例
3.1.属性类型(是String)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//定义此注解保留在字节码中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//设置默认值是"red"
}
@MyAnnotation("java")
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* 这是获得类上的注解,也可以获得方法上的注解,下面就以获得类上的注解为例
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
}
}
}
结果:
value=java
Color=red
从调用的程序中,也可以看出,只有一个属性可以需要赋值的话,可以省略属性名。否则@注解类(属性名=值)
3.2.综合类型
/*枚举类*/
public enum Week{
SUN,MON;
}
/**
* 注解类
*/
public @interface annotationText {
String value();
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//定义此注解保留在字节码中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//设置默认值是"red"
Week week() default Week.MON;//枚举类型
int [] array() default {1,2,3};//数组类型
annotationText annotation() default @annotationText("MY");//注解类型
Class classDemo() default Integer.class;//Class类型
}
@MyAnnotation(value="java",Color="green",week=Week.SUN,array=5,annotation=@annotationText("YOU"),classDemo=String.class)//数组array={4,5,6}
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* 这是获得类上的注解,也可以获得方法上的注解,下面就以获得类上的注解为例
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
System.out.println("week="+annotation.week());
System.out.println("array长度="+annotation.array()。length);
System.out.println("注解类型值="+annotation.annotation()。value());
System.out.println("Class类型值="+annotation.classDemo());
}
}
}
结果:
value=java
Color=green
week=SUN
array长度=1
注解类型值=YOU
Class类型值=classjava.lang.String
4.Method上的注解
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
/**
*注解类
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface annotationText{
Stringvalue();
}
publicclassApplyMyAnnotation{
publicstaticvoidmain(String[]args)throwsException{
Methodmethodshow=ApplyMyAnnotation.class.getMethod("show");
annotationTextanno=methodshow.getAnnotation(annotationText.class);
System.out.println(anno.value());
}
@annotationText("java")
publicvoidshow(){
System.out.println("hello");
}
}
结果:java
猜你喜欢
- 本文实例为大家分享了java实现文件下载的具体代码,供大家参考,具体内容如下public HttpServletResponse downl
- File 类:文件和目录路径名的抽象表示。注意:File 类只能操作文件的属性,文件的内容是不能操作的。1、File 类的字段我们知道,各个
- springboot项目启动慢的问题排查springboot项目,随着时间的推移,启动耗时逐步增加,从几分钟慢慢的达到30多分钟,有点恐怖!
- 创建类第一步新建一个java类QSV,构造函数传入需要解析的文件名称。public class QSV {private RandomAcc
- 本文实例讲述了Java简单验证身份证功能。分享给大家供大家参考,具体如下:package org.cxy.csdn.example;impo
- Java常用类库Math类Math包含用于执行基本数字运算的方法,例如基本指数,对数,平方根和三角函数一、Field SummaryModi
- 1.需求背景需要实现一个动态加载但不显示出来的视图,且该视图上有个动态生成的二维码,最后用其去生成一张快照(也就是图片)。(常见这种情况是来
- @RequestMapping注解注意点类上加没加@RequestMappin注解区别1.如果类上加了 @RequestMappin注解,那
- 1、原理事务的概念想必大家都很清楚,其ACID特性在开发过程中占有重要的地位。同时在并发过程中会出现一些一致性问题,为了解决一致性问题,也出
- Navigator 的 push 和 pop方法Navigator 导航器的 push 和 pop 方法可以携带参数在页面间传递,其他变形的
- MyBatis-Plus不使用数据库默认值的问题有时候我们在设计数据表时希望某些字段使用默认值,比如create_time、和update_
- 简介Springboot Admin是一个管理和监控Springboot项目的组件,分为服务端和客户端,两端通过http进行通信。由于其轻量
- 前言我们都知道,kill在linux系统中是用于杀死进程。kill pid [..]kill命令可将指定的信号发送给相应的进程或工作。 ki
- WebService是一种跨编程语言和跨操作系统平台的远程调用技术所谓远程调用,就是一台计算机a上的一个程序可以调用到另外一台计算机b上的一
- 一、说明Boost.MPI 提供了 MPI 标准(消息传递接口)的接口。该标准简化了并发执行任务的程序的开发。您可以使用线程或通过共享内存或
- 昨天写了一篇Redis布隆过滤器相关的命令的文章,今天来说一说springboot中如何简单在代码中使用布隆过滤器吧。目前市面上也有好几种实
- 引出泛型我们通过如下的示例,引出为什么泛型的概念。public class Test {public static void main(St
- 我们都知道,当RecyclerView数据源更新后,还需要通过adapter调用对应的方法,从而让RecyclerView重新绘制页面本次也
- 顺序语句顺序顾名思义就是程序自上而下执行public class User { public static voi
- 在分支较多的时候,switch的效率比if高,在反汇编中我们即可看到效率高的原因一、switch语句1、在正向编码时,switch语句可以看