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

边栏推荐
- Integer type of C language
- [QT] create mycombobox click event
- Thinkphp6.0 middleware with limited access frequency think throttle
- BUU-Crypto-[GXYCTF2019]CheckIn
- Thread pool: use thread pool to optimize query speed
- VB. Net simple processing pictures, black and white (class library - 7)
- 1.1 history of Statistics
- Flink1.13 SQL basic syntax (I) DDL, DML
- 724. 寻找数组的中心下标
- 如何获取el-tree中所有节点的父节点
猜你喜欢

2022 R2 mobile pressure vessel filling retraining question bank and answers

光模块字母含义及参数简称大全

Google Chrome browser will support the function of selecting text translation

冲击继电器JC-7/11/DC110V
![[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

Kubernets first meeting

Letter meaning and parameter abbreviation of optical module Daquan

VB.net GIF(制作、拆解——优化代码,类库——5)

Introduction to AMBA

谷歌 Chrome 浏览器将支持选取文字翻译功能
随机推荐
2022 question bank and answers for safety management personnel of hazardous chemical business units
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
Arc135 a (time complexity analysis)
BUU-Pwn-test_ your_ nc
拓扑排序和关键路径的图形化显示
力扣(LeetCode)184. 部门工资最高的员工(2022.07.03)
2022 t elevator repair operation certificate examination question bank and simulation examination
[wechat applet] template and configuration (wxml, wxss, global and page configuration, network data request)
win10清除快速访问-不留下痕迹
JS扁平化数形结构的数组
Zzulioj:1201: mode problem
VB.net 调用FFmpeg简单处理视频(类库——6)
BUU-Crypto-[HDCTF2019]basic rsa
How much computing power does transformer have
fastjson
如何判断数组中是否含有某个元素
Flask
Notepad++--显示相关的配置
Graduation design of small programs -- small programs of food and recipes
Online shrimp music will be closed in January next year. Netizens call No