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

所属分类: 软件编程 / java 阅读数: 106
收藏 0 赞 0 分享

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基于反射机制实现全部注解获取的方法示例

这篇文章主要介绍了Java基于反射机制实现全部注解获取的方法,结合实例形式分析了java反射机制获取注解的具体实现方法与操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java 信号量Semaphore的实现

这篇文章主要介绍了Java 信号量Semaphore的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

eclipse+maven+spring mvc项目基本搭建过程

这篇文章主要介绍了eclipse+maven+spring mvc项目基本搭建过程,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring boot集成swagger2生成接口文档的全过程

这篇文章主要给大家介绍了关于Spring boot集成swagger2生成接口文档的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Java冒泡排序法和选择排序法的实现

这篇文章主要介绍了Java冒泡排序法和选择排序法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Cloud Alibaba教程之Sentinel的使用

这篇文章主要介绍了Spring Cloud Alibaba教程之Sentinel的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Josephus环的四种解法(约瑟夫环)基于java详解

这篇文章主要介绍了Josephus环的四种解法(约瑟夫环)基于java详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java继承Thread类创建线程类示例

这篇文章主要介绍了Java继承Thread类创建线程类,结合实例形式分析了java线程操作相关使用技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java使用Callable和Future创建线程操作示例

这篇文章主要介绍了Java使用Callable和Future创建线程操作,结合实例形式分析了java使用Callable接口和Future类创建线程的相关操作技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

springBoot使用JdbcTemplate代码实例

这篇文章主要介绍了springBoot使用JdbcTemplate代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多