当前位置:网站首页>注释与注解
注释与注解
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配置文件

边栏推荐
- Halcon图片标定,使得后续图片处理过后变成与模板图片一样
- Redis realizes ranking function
- Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
- 如何获取el-tree中所有节点的父节点
- 2022 a special equipment related management (elevator) examination questions simulation examination platform operation
- Configure cross compilation tool chain and environment variables
- c语言经典指针和数组笔试题解析
- 2022 question bank and answers for safety management personnel of hazardous chemical business units
- Upper computer software development - log information is stored in the database based on log4net
- Just do it with your hands 7 - * project construction details 2 - hook configuration
猜你喜欢

Halcon图片标定,使得后续图片处理过后变成与模板图片一样

Graduation design of small programs -- small programs of food and recipes

VB. Net simple processing pictures, black and white (class library - 7)

(4) Canal multi instance use

检漏继电器JY82-2P

Principle and practice of common defects in RSA encryption application
![[high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk](/img/b9/cf4db4f8a5d2ef3fb344258f0e30f5.jpg)
[high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk

ANSYS command
![[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre](/img/3e/b5df691ca1790469eb1b4e8ea5b4c0.png)
[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre

Kubernets first meeting
随机推荐
ansys命令
基于单片机的太阳能杀虫系统
Excel comparator
Letter meaning and parameter abbreviation of optical module Daquan
left_and_right_net可解释性设计
left_ and_ right_ Net interpretable design
ETCD数据库源码分析——初始化总览
Descriptive analysis of data distribution characteristics (data exploration)
Graduation design of small programs -- small programs of food and recipes
如何展开Collapse 的所有折叠面板
VB.net GIF(制作、拆解——优化代码,类库——5)
Just do it with your hands 7 - * project construction details 2 - hook configuration
1480. Dynamic sum of one-dimensional array
光模块字母含义及参数简称大全
flink1.13 sql基础语法(二)join操作
How to use postman to realize simple interface Association [add, delete, modify and query]
LM小型可编程控制器软件(基于CoDeSys)笔记二十二:错误4268/4052
【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
Redis realizes ranking function
ANSYS command