当前位置: 移动技术网 > IT编程>开发语言>Java > java教程之java注解annotation使用方法

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网