注解
注解
什么是注解
-
Annotation是从JDK5.0开始引入的新技术.
-
Annotation的作用:
- 不是程序本身,可以对程序作出解释.(这一点和注释(comment)没什么区别)可
- 以被其他程序(比如:编译器等)读取.
-
Annotation的格式:
- 注解是以"@注释名"在代码中存在的﹐还可以添加一些参数值﹐例如:@SuppressWarnings(value="unchecked").
-
Annotation在哪里使用?
- 可以附加在package , class , method , field等上面,相当于给他们添加了额外的辅助信息,我们可以通过反射机制编程实现对这些元数据的访问
内置注解
- @Override:定义在java.lang.Override中,此注释只适用于修辞方法,表示一个方法声明打算
重写超类中的另一个方法声明. - @Deprecated:定义在java.lang.Deprecated中,此注释可以用于修辞方法﹐属性﹐类,表示不鼓励程序员使用这样的元素﹐通常是因为它很危险或者存在更好的选择.
- @suppressWarnings:定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息.
与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参数都是已经定义好了的,我们选择性的使用就好了.- @SuppressWarnings("all")
- @SuppressWarnings("unchecked")
- @SuppressWarnings(value={"unchecked" ,"deprecation"})
- 等等.....
@Deprecated//过时的
public static void test(){
System.out.println("ooo");
}
public void test1(){
test();
}
元注解
- 元注解的作用就是负责注解其他注解,Java定义了4个标准的
meta-annotation类型,他们被用来提供对其他annotation类型作说明. - 这些类型和它们所支持的类在java.lang.annotation包中可以找到.(@Target , @Retention ,@Documented , @Inherited )
- @Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
- @Retention:表示需要在什么级别保存该注释信息﹐用于描述注解的生命周期
(SOURCE< CLASS< RUNTIME) - @Document:说明该注解将被包含在javadoc中
- @lInherited:说明子类可以继承父类中的该注解
@MyAnnotation
public void test1() {
System.out.println("111");
}
}
//定义一个注解
//Target表示我们的注解可以用在哪些地方.
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// Retention表示我们的注解在什么地方还有效。 \保持,保留;
// /runtime>class>sources
@Retention(value = RetentionPolicy.RUNTIME)
// Documented表示是否将我们的注解生成在JAVAdoc中 \文档记录
@Documented
//Inherited子类可以继承父类的注解 \遗传的;(财产)继承的
@Inherited
@interface MyAnnotation {
}
自定义注解
- 使用@interface自定义注解时﹐自动继承了java.lang.annotation.Annotation接口
- 分析:
- @interface用来声明一个注解﹐格式:public @interface注解名
- 其中的每一个方法实际上是声明了一个配置参数.
- 方法的名称就是参数的名称.
- 返回值类型就是参数的类型(返回值只能是基本类型,Class , String , enum ).√可以通过default来声明参数的默认值
- 如果只有一个参数成员,一般参数名为value
- 注解元素必须要有值,我们定义注解元素时﹐经常使用空字符串,0作为默认值.
package com.xin.other.demo01;
import java.lang.annotation.*;
import java.lang.reflect.Field;
///练习反射操作注解
public class Day91504 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class<?> c1 = Class.forName("com.xin.other.demo01.Student2");
//通过反射获得注解
Annotation[] annotation = c1.getAnnotations();
for (Annotation annotation1:
annotation) {
System.out.println(annotation1);//@com.xin.other.demo01.MyXin1(value=db_student)
}
//获得注解的value的值。
MyXin1 myXin1=c1.getAnnotation(MyXin1.class);
String value = myXin1.value();
System.out.println(value);//db_student
//获得类指定的注解
Field name = c1.getDeclaredField("name");
MyXin2 myXin2 = name.getAnnotation(MyXin2.class);
System.out.println(myXin2.columnName());//db_name
System.out.println(myXin2.type());
System.out.println(myXin2.length());
}
}
@MyXin1("db_student")
class Student2{
@MyXin2(columnName = "db_id",type = "int",length = 10)
private int id;
@MyXin2(columnName = "db_age",type = "int",length = 10)
private int age;
@MyXin2(columnName = "db_name",type = "varchar",length = 3)
private String name;
public Student2() {
}
public Student2(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student2{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyXin1{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyXin2{
String columnName();
String type();
int length();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?