当前位置: 首页 > news >正文

第九天:java注解

注解

1 什么是注解(Annotation)

public class Test01 extends Object{//@Override重写的注解@Overridepublic String toString() {return "Test01{}";}
}

2 内置注解

2.1 @Override

@Override重写的注解

@Override
public String toString() {return "Test01{}";
}

2.2 @Deprecated

@Deprecated 不推荐程序员使用,但是可以使用,存在更好的方法.

@Deprecated
public static void test(){System.out.println("Test01");
}

2.3 @SuppressWarnings

@SuppressWarnings 镇压警告,平时写代码不建议镇压警告。

@SuppressWarnings("all")
public static void test02(){List list = new ArrayList();
}

3 元注解

元注解作用:负责注解其他注解。重点掌握,@Target,@Retention

3.1 定义一个注解

@interface MyAnnotation {}

3.2 @Target

@Target 表示我们的注解可以用在哪些地方

//Target注解的作用:注解自定义的interface注解
//只能在方法,类上使用
@Target({ElementType.METHOD,ElementType.TYPE})
@interface MyAnnotation {}

3.3 @Retention

@Retention用以描述注解的生命周期

SOURCE源码级别,CLASS编译之后,

RUNTIME运行时(用的最多)

自定义的类一般都写RUNTIME

注解在什么阶段有效
//runtime>class>source
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {}

3.4 @Documented

@Documented 表示是否将我们的注解生成在JavaDoc中。

//Documented 表示是否将我们的注解生成在JavaDoc中
@Documented
@interface MyAnnotation {}

3.5 @Inherited

@Inherited 子类可以继承父类的注解

//Inherited 子类可以继承父类的注解
@Inherited
@interface MyAnnotation {}
//Target注解的作用:注解自定义的interface注解
//只能在方法,类上使用
@Target({ElementType.METHOD,ElementType.TYPE})//Retention表示注解在什么阶段有效
//runtime>class>source
@Retention(RetentionPolicy.RUNTIME)//Documented 表示是否将我们的注解生成在JavaDoc中
@Documented//Inherited 子类可以继承父类的注解
@Inherited//定义一个注解
@interface MyAnnotation {}

4 自定义注解

此时并未给注解赋默认值,因此在类或方法上使用该注解时得传入一个值。

public class Test03 {@MyAnnotation1(name = "韩立")public void test(){}
}
//自定义注解
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {//注解的参数:参数类型+参数名+();String name();
}@interface MyAnnotation1 {//注解的参数:参数类型+参数名+();String name();
}

此时注解默认值为空,在类或方法上使用该注解时可不用传入一个值。

public class Test03 {@MyAnnotation1()public void test(){}
}
//自定义注解
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {//注解的参数:参数类型+参数名+();String name() default " ";
}
public class Test03 {@MyAnnotation1()public static void test(){}@MyAnnotation2(value = "hh")public static void test2(){}public static void main(String[] args) {test();}
}@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
自定义注解
@interface MyAnnotation1 {//注解的参数:参数类型+参数名+();并不是方法String name() default " ";int age() default 0;int id() default -1;//若默认值为-1,代表不存在String [] school() default {"清华"};
}@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
//自定义注解
@interface MyAnnotation2 {String value();
}

;//若默认值为-1,代表不存在
String [] school() default {“清华”};
}

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
//自定义注解
@interface MyAnnotation2 {
String value();
}

相关文章:

  • vue3路由跳转的三种方式
  • lidar和imu的标定(三)平面约束的方法
  • 无法运用pytorch环境、改环境路径、隔离环境
  • @Value,@ConfigurationProperties
  • Go 即时通讯系统:客户端与服务端 WebSocket 通信交互
  • lidar和imu的标定(一)Robust Real-time LiDAR-inertial Initialization
  • 2022—2025年:申博之路及硕士阶段总结
  • 4、数据标注的武林秘籍:Label-Studio vs CVAT vs Roboflow
  • Python编程基础(二)| 列表简介
  • SOC-ESP32S3部分:24-WiFi配网
  • Java八股文——Java基础「面向对象篇」
  • 【js逆向_AES】全国二手房指数数据爬取
  • SolidWorks软件的安装与卸载
  • 研读论文《Attention Is All You Need》(13)
  • 深入理解 C++11 中的 std::move —— 移动语义详解(小白友好版)
  • 3C All-in-One Toolbox:安卓手机的全能维护专家
  • Vehicle HAL(2)--Vehicle HAL 的启动
  • DDR5 ECC详细原理介绍与基于协议讲解
  • IEE754标准,double和int转换,在线计算器
  • pyqt5笔记20250601
  • 网站一般怎么维护/湖南网站优化
  • 好玩的html代码/重庆seo教程博客
  • 广东省最差的三座城市/seo搜索引擎优化报价
  • 网页在线代理服务器/湖南seo推广多少钱
  • 网站做关键词排名每天要做什么/今日最近的新闻大事10条
  • 创建网站的好处/什么是seo关键词