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

第九天: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();
}

http://www.lryc.cn/news/2395298.html

相关文章:

  • 十一、【核心功能篇】测试用例管理:设计用例新增编辑界面
  • react-native的token认证流程
  • ERP系统中商品定价功能设计:支持渠道、会员与批发场景的灵活定价机制
  • Spring是如何实现属性占位符解析
  • 数据结构之ArrayList
  • DDR4读写压力测试
  • uniapp 开发企业微信小程序时,如何在当前页面真正销毁前或者关闭小程序前调用一个api接口
  • WPF 按钮点击音效实现
  • 编写测试用例
  • 解释程序(Python)不需要生成机器码 逐行解析 逐行执行
  • 每日Prompt:隐形人
  • TensorFlow深度学习实战(19)——受限玻尔兹曼机
  • 告别手动绘图!基于AI的Smart Mermaid自动可视化图表工具搭建与使用指南
  • 【Oracle】安装单实例
  • C++测开,自动化测试,业务(第一段实习)
  • QT中更新或添加组件时出现“”qt操作至少需要一个处于启用状态的有效资料档案库“解决方法”
  • 论文速读《UAV-Flow Colosseo: 自然语言控制无人机系统》
  • ES6+中Promise 中错误捕捉详解——链式调用catch()或者async/await+try/catch
  • CDN安全加速:HTTPS加密最佳配置方案
  • 解常微分方程组
  • C++实现汉诺塔游戏自动完成
  • 在 ABP VNext 中集成 Serilog:打造可观测、结构化日志系统
  • pikachu靶场通关笔记07 XSS关卡03-存储型XSS
  • GitLab CI、GitHub Actions和Jenkins进行比较
  • strcat及其模拟实现
  • OpenCV CUDA模块直方图计算------用于在 GPU 上执行对比度受限的自适应直方图均衡类cv::cuda::CLAHE
  • 华为OD机试真题——矩形绘制(2025A卷:200分)Java/python/JavaScript/C/C++/GO最佳实现
  • 通义开源视觉感知多模态 RAG 推理框架 VRAG-RL:开启多模态推理新时代
  • 爬虫入门:从基础到实战全攻略
  • qemu安装risc-V 64