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

边栏推荐
- JS string splicing
- LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703
- Kubernets first meeting
- Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
- Accidentally deleted the data file of Clickhouse, can it be restored?
- Wechat applet +php realizes authorized login
- The difference between PX EM rem
- VB.net 简单的处理图片,黑白(类库——7)
- ANSYS command
- 2022g2 power station boiler stoker special operation certificate examination question bank and answers
猜你喜欢

接地继电器DD-1/60

LM small programmable controller software (based on CoDeSys) note 22: error 4268/4052

Topological sorting and graphical display of critical path

【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块

Input displays the currently selected picture

RSA加密应用常见缺陷的原理与实践
![[MySQL practice of massive data with high concurrency, high performance and high availability -8] - transaction isolation mechanism of InnoDB](/img/8e/90d08d22a2d340242be2357f662ea4.jpg)
[MySQL practice of massive data with high concurrency, high performance and high availability -8] - transaction isolation mechanism of InnoDB
![[wechat applet] template and configuration (wxml, wxss, global and page configuration, network data request)](/img/78/63ab1a8bb1b6e256cc740f3febe711.jpg)
[wechat applet] template and configuration (wxml, wxss, global and page configuration, network data request)
![[microservice] Nacos cluster building and loading file configuration](/img/50/7af220c57a06eb186729c9882d9dab.png)
[microservice] Nacos cluster building and loading file configuration

VB.net GIF(制作、拆解——优化代码,类库——5)
随机推荐
flink1.13 sql基础语法(二)join操作
1480. Dynamic sum of one-dimensional array
Flask
Take you to quickly learn how to use qsort and simulate qsort
left_ and_ right_ Net normal version
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
Graduation design of small programs -- small programs of food and recipes
px em rem的区别
js获取对象中嵌套的属性值
VB.net 简单的处理图片,黑白(类库——7)
Redis realizes ranking function
The difference between PX EM rem
VB.net 调用FFmpeg简单处理视频(类库——6)
Configure cross compilation tool chain and environment variables
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
Enterprise level log analysis system elk (if things backfire, there must be other arrangements)
云原生架构实战案例及优化解决方案
JS string splicing enhancement
如何判断数组中是否含有某个元素
SQL performance optimization skills