JAVA之注解(Annotation)

本文详细介绍了如何在Java中自定义Annotation注解类型,如何在类源代码中引用并利用反射机制获取注解信息,包括标识型注解、成员定义和JDK内置注解的应用。通过实例演示了如何在Person类中使用这些注解,并进行了课堂总结和思考题讨论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

write:2022-3-29
update:2022

注解机制

有没有办法让运行中的Java程序能读取一个Java类的注解信息呢?从JDK5开始,提供了一种灵活的注解机制,它允许Java程序在运行时读取类的注解信息。
运用注解机制主要包含以下步骤:
(1)自定义Annotation注解类型。
(2)在类的源代码中引用注解类型。
(3)在程序中运用反射机制读取类的注解信息。

自定义Annotation注解类型

  1. 注解类型也属于一种Java类型,它用“@interface”关键字来声明。eg:public @interface MyAnnotation{} //声明注解类
    注意与接口关键字区别:定义接口:public interface MyIFC{…} //声明接口

  2. 当MyAnnotation注解类的类体为空“{}”,不包含任何成员,这样的注解称为标识型注解(Marked Annotation)。
    此外,在声明注解类型时,可以定义一些成员。
    public @interface MyAnnotation{
    String value();
    }

  3. “default”关键字:
    下面的代码定义了包含两个成员的MyAnnotation类:
    public @interface MyAnnotation{
    String value() default “默认构造方法”;
    Class type() default void.class;
    }
    以上“default”关键字用于为成员设定默认值。例如type成员的默认值为“void.class”。

  4. 在定义注解类型时,还可以引用JDK内置的一些注解类型来进行相关的限定。
    @Target注解:用来指定当前注解所适用的目标。(有可能是类,构造方法,成员变量等等)
    @Rentention注解:指定当前注解的有效范围。(有以下三种)
    在这里插入图片描述
    @Documented注解:标识性注解。表示注解类型包含的信息会被加入到JavaDoc文档中。

  5. eg:

先定义三个注解类型:

package Annotation;

import java.lang.annotation.*;

@Documented
@Target(ElementType.TYPE)//使用与java类型
@Retention(RetentionPolicy.RUNTIME)//有效范围是运行时
public @interface AuthorAnnotation {
    String name();
    String company();
}

package Annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.CONSTRUCTOR)  //适用于构造方法
@Retention(RetentionPolicy.RUNTIME)  //有效范围是运行时
public @interface ConstructorAnnotation {
    String value() default "默认构造方法";   //value字段的默认值是:"默认构造方法"
}

package Annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//适用于成员变量、成员方法和参数
@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)  //有效范围是运行时
public @interface CommonAnnotation {
    Class type() default void.class;
    String description();
}

在类的源代码中引用注解

前面定义的注解根据其引用目标和范围等,引用到类中

Person类:

package Annotation;

@AuthorAnnotation(name = "Tom",company = "ABC Company")
public class Person {
    @CommonAnnotation(type = String.class,description = "姓名")
    private String name;
    @CommonAnnotation(type=int.class,description="年龄")
    private int age;
    @ConstructorAnnotation
    public Person(){
        this("unknown",0);
    }
    @ConstructorAnnotation("带参数的构造方法")
    public Person( //注解构造方法的参数
                   @CommonAnnotation(type=String.class,description="姓名")String name,
                   @CommonAnnotation(type=int.class,description="年龄")int age) {
        this.name = name;
        this.age = age;
    }
    @CommonAnnotation(type=String.class,description="获得姓名")
    public String getName() {
        return name;
    }
    @CommonAnnotation(description="设置姓名")
    public void setName(@CommonAnnotation(type=String.class,description="姓名")String name) {
        this.name = name;
    }
    @CommonAnnotation(type=int.class,description="获得年龄")
    public int getAge() {
        return age;
    }
    @CommonAnnotation(description="设置年龄")
    public void setAge(
            @CommonAnnotation(type=int.class,description="年龄")int age){
        this.age=age;
    }

}

在程序中运用反射机制读取类的注解信息

在这里插入图片描述

课堂小结

在这里插入图片描述

思考题

2.以下哪些是JDK内置的标识型注解?
a) @Target
b) @Documented
c) @Override
d) @Rentention
[答案] b,c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值