s-day12-元注解

This commit is contained in:
xx
2026-10-27 11:47:37 +08:00
parent 6afa427872
commit 718ef90dbb
3 changed files with 27 additions and 3 deletions
@@ -0,0 +1,15 @@
package com.inmind.annotation02;
/*
元注解:用来修饰注解的注解
@Target:设置某一个注解的作用范围
ElementType.TYPE
ElementType.FIELD
ElementType.CONSTRUCTOR
ElementType.METHOD
@Retention:设置某一个注解的生命周期
SOURCE:编译时期有效
CLASS:编译后,运行前有效
RUNTIME:运行时有效 (比较常用,结合反射操作)
*/
public class Demo03 {
}
@@ -1,4 +1,10 @@
package com.inmind.annotation02;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/*
9.自定义注解
@@ -22,6 +28,8 @@ package com.inmind.annotation02;
b.属性的默认值值可以覆盖
*/
@Target(value = {ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String name() default "张三";//注解的属性
int value();
@@ -1,5 +1,6 @@
package com.inmind.annotation02;
@MyAnnotation(value = 1,name = "abc")
public class Student {
@MyAnnotation(value = 1,name = "李四")
String name;
@@ -7,7 +8,7 @@ public class Student {
int age;
@MyAnnotation(1)
public Student(String name, @MyAnnotation(1) int age) {
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@@ -17,7 +18,7 @@ public class Student {
return name;
}
public @MyAnnotation(1) void setName(String name) {
public void setName(String name) {
this.name = name;
}