From 6afa427872b2b75d35faa5a3d8b0926cb5a53826 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Tue, 27 Oct 2026 11:24:44 +0800 Subject: [PATCH] =?UTF-8?q?s-day12-=E8=87=AA=E5=AE=9A=E4=B9=89=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/inmind/annotation02/Demo02.java | 14 ++++++++ .../com/inmind/annotation02/MyAnnotation.java | 28 ++++++++++++++++ .../src/com/inmind/annotation02/Student.java | 32 +++++++++++++++++++ s-day12/src/com/inmind/junit01/Demo01.java | 1 + 4 files changed, 75 insertions(+) create mode 100644 s-day12/src/com/inmind/annotation02/Demo02.java create mode 100644 s-day12/src/com/inmind/annotation02/MyAnnotation.java create mode 100644 s-day12/src/com/inmind/annotation02/Student.java diff --git a/s-day12/src/com/inmind/annotation02/Demo02.java b/s-day12/src/com/inmind/annotation02/Demo02.java new file mode 100644 index 0000000..3c7d036 --- /dev/null +++ b/s-day12/src/com/inmind/annotation02/Demo02.java @@ -0,0 +1,14 @@ +package com.inmind.annotation02; + +import java.util.Date; + +/* + java中的四大引用类型:类,接口,注解,枚举(可以查看Class类) +JDK中的注解 + @override:验证方法是否是重写 + @FunctionalInterface:验证是否是函数式接口 + @SuppressWarnings:抑制警告 + @Deprecated:设置为过时 + */ +public class Demo02 { +} diff --git a/s-day12/src/com/inmind/annotation02/MyAnnotation.java b/s-day12/src/com/inmind/annotation02/MyAnnotation.java new file mode 100644 index 0000000..e0e437e --- /dev/null +++ b/s-day12/src/com/inmind/annotation02/MyAnnotation.java @@ -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(); +} diff --git a/s-day12/src/com/inmind/annotation02/Student.java b/s-day12/src/com/inmind/annotation02/Student.java new file mode 100644 index 0000000..ecd96e0 --- /dev/null +++ b/s-day12/src/com/inmind/annotation02/Student.java @@ -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; + } + +} diff --git a/s-day12/src/com/inmind/junit01/Demo01.java b/s-day12/src/com/inmind/junit01/Demo01.java index 0898215..accaf1f 100644 --- a/s-day12/src/com/inmind/junit01/Demo01.java +++ b/s-day12/src/com/inmind/junit01/Demo01.java @@ -33,6 +33,7 @@ import org.junit.Test;//导包 注解(自定义注解)+反射(识别注解+解析注解) */ +@SuppressWarnings("all") public class Demo01 { long start;