服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - java教程之java注解annotation使用方法

java教程之java注解annotation使用方法

2019-11-05 11:26java技术网 JAVA教程

这篇文章主要介绍了java注解annotation使用方法,注解可以定义到方法上,类上,一个注解相当与一个类,就相当于实例了一个对象,加上了注解,就相当于加了一个标志

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

延伸 · 阅读

精彩推荐