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,注解,annotation
0
投稿

猜你喜欢

  • 详解IDEA中SpringBoot整合Servlet三大组件的过程

    2023-05-06 15:23:13
  • 分别用ToolBar和自定义导航栏实现沉浸式状态栏

    2023-03-17 21:08:14
  • Java如何把数组转换为ArrayList

    2021-08-14 19:59:45
  • Android 配置gradle实现VersionCode自增实例

    2021-07-03 12:39:23
  • 简单介绍java中equals以及==的用法

    2023-01-28 07:47:41
  • Android 监听应用前/后台切换实例代码

    2021-06-05 05:25:22
  • MyBatis多对多关联映射创建示例

    2023-08-09 06:40:10
  • Java多线程案例之单例模式懒汉+饿汉+枚举

    2021-11-07 05:18:01
  • C# TrieTree介绍及实现方法

    2022-02-10 22:04:53
  • 用c#获得当前用户的Application Data文件夹位置

    2022-02-13 10:05:22
  • C#发送数据到剪贴板及从剪贴板中取数据的方法

    2021-06-06 15:28:38
  • Android布局技巧之合并布局

    2021-10-27 11:12:57
  • C#采用mouse_event函数实现模拟鼠标功能

    2023-07-07 16:09:54
  • 详解Java数据库连接JDBC基础知识(操作数据库:增删改查)

    2023-08-22 23:47:37
  • Java输入年份和月份判断多少天实例代码

    2023-12-23 10:43:11
  • JavaScript 与 Java 区别介绍 学java怎么样

    2023-11-25 12:23:49
  • Android倒计时控件 Splash界面5秒自动跳转

    2023-09-24 18:41:16
  • SSH框架网上商城项目第8战之查询和删除商品类别功能实现

    2023-02-12 05:54:39
  • C#预处理器指令的用法实例分析

    2023-03-09 16:21:07
  • Java Socket 编程详解

    2022-06-13 12:06:57
  • asp之家 软件编程 m.aspxhome.com