当前位置:网站首页>注释与注解
注释与注解
2022-07-04 05:32:00 【长安有故里y】
1 注释
注释的分类
单行注释: //
多行注释: /* */
文档注释:/** */
作用:传递额外的信息,给程序员看,不参与编译
2 注释
Annotation是代码里边的特殊的标记,可以在编译、类加载、运行的时候被读取
,并执行相应的处理。
2.1 注释与注解的不同

2.2 自定义注解
package com.yq.day72;
public @interface MyAnno {
//属性类型的范围 基本的数据类型 string class 枚举类型 注解类型
// 以及以上类型的数组
int age();
double price();
String name();
String[] names();
Class c();
MyAnno3 anno();
// 注解不能继承
}
@interface MyAnno3 {
}
注解不能被继承
2.3 注解的使用
实例化对象不是通过new,而是 属性名=属性值
使用 的时候 给每个属性完成赋值
@注解名(属性名1=value,属性名2=value,....)
注意:
1.每个属性都要赋值
2.有默认值可以不用
3.只有一个属性,并且为value,可以简化赋值
4.数组的复制使用{}
5.引用类型的值不能是null
2.4 注解处理器
结合反射去应用
使用的具体的步骤
1.通过反射获取注册的信息
2.获取文件字节码对象
3.获取对象的方法,判断上面的方法是否使用了注解
4.使用了获取注解的实例
5.获取属性值,进行操作
使用的方法:属于类Class
| boolean isAnnotation() | 如果此 Class 对象表示一个注释类型则返回 true。 |
|---|---|
| A getAnnotation(Class annotationClass) | 如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。 |
代码实例:
package com.yq.day72;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
/** * @author [email protected] * @description * @since 2022/07/02 11:41 * 创建一个学会对象,不使用new的方式,使用反射和注解实现 * name的长度在5之间 ,年龄在18-25之间 */
//测试类
public class Demo04 {
//定义一个字节码的文件对象,进行赋值
static Class<?> aClass;
static {
try {
aClass = Class.forName("com.yq.day72.Student");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException, InstantiationException, IllegalAccessException {
Student zhansa = getInstance("zga", 293);
System.out.println(zhansa);
}
public static Student getInstance(String name,int age) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
//名字的校验 获取student的属性name
Field name1 = aClass.getDeclaredField("name");
//判断是否使用注解 判断属性name上是否有注解
boolean a1 = name1.isAnnotationPresent(NameLimit.class);
if (a1){
//获取注解的实例
NameLimit a2 = name1.getAnnotation(NameLimit.class);
//获取注解的属性值
int length = a2.length();
//判断
if (name.length()>length){
throw new IllegalArgumentException("长度大");
}
}
//对年龄的校验 获取age的属性
Field age1 = aClass.getDeclaredField("age");
//判断是否有注解
boolean annotationPresent = age1.isAnnotationPresent(AgeLimit.class);
if (annotationPresent){
//获取注解的实例
AgeLimit annotation = age1.getAnnotation(AgeLimit.class);
//获取属性值
int i = annotation.maxAge();
int i1 = annotation.minAge();
if (age>i || age <i1){
throw new IllegalArgumentException("年龄不合法!!!!!");
}
}
//获取构造方法
Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class, int.class);
//忽略限制
declaredConstructor.setAccessible(true);
//创建学生对象
Student o = (Student) declaredConstructor.newInstance(name, age);
return o;
}
}
class Student{
//注解
@NameLimit
String name;
//注解
@AgeLimit
int age;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
private Student(String name, int age) {
this.name = name;
this.age = age;
}
}
//元注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface NameLimit{
int length() default 5;
}
//元注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface AgeLimit{
int maxAge() default 25;
int minAge() default 18;
}
2.5 元注解
描述注解的注解,元数据 meta data
常用的元注解
**@Retention元注解,来定义我们自己定义的注解的保留级别.**
- RetentionPolicy.RUNTIME
- RetentionPolicy.CLASS 默认
- RetentionPolicy.SOURCE
**@Target元注解,注解可以作用的目标**
对于注解而言,可以作用的目标:
1. 整个类 ElementType.TYPE
2. 成员变量 ElementType.FIELD
3. 构造方法 ElementType.CONSTRUCTOR
4. 成员方法 ElementType.METHOD
2.6 注解VS配置文件

边栏推荐
- 1.1 history of Statistics
- 检漏继电器JY82-2P
- The end of the Internet is rural revitalization
- 724. 寻找数组的中心下标
- FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
- Write a complete answer applet (including single choice questions, judgment questions and multiple topics) (III) single choice questions, judgment questions, and the first question display
- 一键过滤选择百度网盘文件
- Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage
- Canoe panel learning video
- 如何展开Collapse 的所有折叠面板
猜你喜欢

数据标注是一块肥肉,盯上这块肉的不止中国丨曼孚科技
![[QT] create mycombobox click event](/img/5a/ed17567a71f6737891fc7a8273df0a.png)
[QT] create mycombobox click event

transformer坑了多少算力

724. 寻找数组的中心下标

BUU-Reverse-easyre

2022 a special equipment related management (elevator) examination questions simulation examination platform operation

2022 t elevator repair operation certificate examination question bank and simulation examination

Flask

Just do it with your hands 7 - * project construction details 2 - hook configuration

HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
随机推荐
力扣(LeetCode)184. 部门工资最高的员工(2022.07.03)
光模块字母含义及参数简称大全
VB. Net GIF (making and disassembling - optimizing code, class library - 5)
Signification des lettres du module optique et abréviation des paramètres Daquan
left_and_right_net可解释性设计
How much computing power does transformer have
win10清除快速访问-不留下痕迹
left_and_right_net正常版本
【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
JS string splicing enhancement
[paper summary] zero shot semantic segmentation
Arc135 a (time complexity analysis)
[high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk
One click filtering to select Baidu online disk files
1480. Dynamic sum of one-dimensional array
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
ansys命令
Etcd database source code analysis - initialization overview
Notepad++ -- display related configurations
[wechat applet] template and configuration (wxml, wxss, global and page configuration, network data request)