From 718ef90dbb92e841cbfe6d258f6058220fccb7df Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Tue, 27 Oct 2026 11:47:37 +0800 Subject: [PATCH] =?UTF-8?q?s-day12-=E5=85=83=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- s-day12/src/com/inmind/annotation02/Demo03.java | 15 +++++++++++++++ .../src/com/inmind/annotation02/MyAnnotation.java | 8 ++++++++ s-day12/src/com/inmind/annotation02/Student.java | 7 ++++--- 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 s-day12/src/com/inmind/annotation02/Demo03.java diff --git a/s-day12/src/com/inmind/annotation02/Demo03.java b/s-day12/src/com/inmind/annotation02/Demo03.java new file mode 100644 index 0000000..cecc9da --- /dev/null +++ b/s-day12/src/com/inmind/annotation02/Demo03.java @@ -0,0 +1,15 @@ +package com.inmind.annotation02; +/* +元注解:用来修饰注解的注解 + @Target:设置某一个注解的作用范围 + ElementType.TYPE + ElementType.FIELD + ElementType.CONSTRUCTOR + ElementType.METHOD + @Retention:设置某一个注解的生命周期 + SOURCE:编译时期有效 + CLASS:编译后,运行前有效 + RUNTIME:运行时有效 (比较常用,结合反射操作) + */ +public class Demo03 { +} diff --git a/s-day12/src/com/inmind/annotation02/MyAnnotation.java b/s-day12/src/com/inmind/annotation02/MyAnnotation.java index e0e437e..512d0b7 100644 --- a/s-day12/src/com/inmind/annotation02/MyAnnotation.java +++ b/s-day12/src/com/inmind/annotation02/MyAnnotation.java @@ -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(); diff --git a/s-day12/src/com/inmind/annotation02/Student.java b/s-day12/src/com/inmind/annotation02/Student.java index ecd96e0..608fc9e 100644 --- a/s-day12/src/com/inmind/annotation02/Student.java +++ b/s-day12/src/com/inmind/annotation02/Student.java @@ -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; }