s-day12-自定义注解

This commit is contained in:
xx
2026-10-27 11:24:44 +08:00
parent 5af648e2c0
commit 6afa427872
4 changed files with 75 additions and 0 deletions
@@ -0,0 +1,14 @@
package com.inmind.annotation02;
import java.util.Date;
/*
java中的四大引用类型:类,接口,注解,枚举(可以查看Class类)
JDK中的注解
@override:验证方法是否是重写
@FunctionalInterface:验证是否是函数式接口
@SuppressWarnings:抑制警告
@Deprecated:设置为过时
*/
public class Demo02 {
}
@@ -0,0 +1,28 @@
package com.inmind.annotation02;
/*
9.自定义注解
1.注解的关键字:@interface
2.注解中可以定义属性
定义属性的格式: 数据类型 属性名();
设置默认值的属性的格式:数据类型 属性名() default 属性值;
3.注解的属性的类型有限制
八大基本类型 String Class 注解 枚举
以上类型的一维数组
4.注解中有一个特殊的属性名value
当属性赋值时,如果只有一个value属性在赋值那么value=是可以省略的
5.注解如何使用:它可以修饰类,构造方法,成员变量,成员方法
6.注解在使用中的注意事项:
a.属性值必须赋值
b.属性的默认值值可以覆盖
*/
public @interface MyAnnotation {
String name() default "张三";//注解的属性
int value();
}
@@ -0,0 +1,32 @@
package com.inmind.annotation02;
@MyAnnotation(value = 1,name = "abc")
public class Student {
@MyAnnotation(value = 1,name = "李四")
String name;
@MyAnnotation(10)
int age;
@MyAnnotation(1)
public Student(String name, @MyAnnotation(1) int age) {
this.name = name;
this.age = age;
}
@MyAnnotation(1)
public String getName() {
return name;
}
public @MyAnnotation(1) void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
@@ -33,6 +33,7 @@ import org.junit.Test;//导包
注解(自定义注解)+反射(识别注解+解析注解)
*/
@SuppressWarnings("all")
public class Demo01 {
long start;