注解 Annotation

本文深入解析Java注解的原理与应用,涵盖注释与注解的区别、常见注解的使用、自定义注解的创建、元注解的功能、反射机制读取注解信息以及单元测试的实践。

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

注释:提高程序的可读性,对程序运行没有影响。

注解: 给程序带上一些标记,从而影响编译器运行程序的结果!

 

注解的作用:

1)可以在程序上(类、方法、属性)携带信息

2) 通过注解简化(取代)配置文件(xml或者properties文件)

 

1 常见注解


//告诉编译器强制对方法进行覆盖(重写)

@Override
public String toString() {

    return super.toString();

}

//告诉编译器压制警告

@SuppressWarnings("rawtypes")
public List save(){

    List list = new ArrayList();

    return list;

}

//提示方法过时

@Deprecated
public void update(){

}

 

2 注解语法

public @interface Override {

}

 

3 自定义注解

public @interface Author {

//声明属性

//1)属性的类型可以是基本类型也可以是数组类型

//2)使用default给注解属性一个默认值

// String name();

// String[] name() default "Lynn";

//3)如果属性名称为value的话,在引用的时候可以不写名value

// 一定是第一个。

String value();

}

 

4 元注解

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})

声明注解的使用范围:

  • ElemenetType.CONSTRUCTOR       构造器声明
  • ElemenetType.FIELD          域声明(包括 enum 实例)
  • ElemenetType.LOCAL_VARIABLE     局部变量声明
  • ElemenetType.METHOD         方法声明
  • ElemenetType.PACKAGE         包声明
  • ElemenetType.PARAMETER       参数声明
  • ElemenetType.TYPE          类,接口(包括注解类型)或enum声明
  • ElementType.ANNOTATION_TYPE     注解

@Retention(RetentionPolicy.SOURCE)

声明注解的有效范围:

  • RetentionPolicy.SOURCE       注解将被编译器丢弃
  • RetentionPolicy.CLASS        注解在class文件中可用,但会被JVM丢弃
  • RetentionPolicy.RUNTIME         JVM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。

@Documented   将此注解包含在 javadoc 中

@Inherited   允许子类继承父类中的注解

 

5 反射注解

//自定义注解
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String value() default "";
}

//测试
@MyAnnotation(value = "类注解")
public class test {
    @MyAnnotation(value = "字段注解")
    private String id;
    @MyAnnotation(value = "方法注解")
    public void test(@MyAnnotation(value = "参数注解") String id){

    }
    @Test
    public void t() throws Exception{
        MyAnnotation annotationC = test.class.getAnnotation(MyAnnotation.class);
        String Cvalue = annotationC.value();
        System.out.println(Cvalue);
        Field field = test.class.getDeclaredField("id");
        MyAnnotation Fannotation = field.getAnnotation(MyAnnotation.class);
        String Fvalue = Fannotation.value();
        System.out.println(Fvalue);
        Method methodtest = test.class.getMethod("test",String.class);
        MyAnnotation Mannotation = methodtest.getAnnotation(MyAnnotation.class);
        String Mvalue = Mannotation.value();
        System.out.println(Mvalue);
        Annotation[][] parameterAnnotations = methodtest.getParameterAnnotations();
        for (Annotation[] annotations:parameterAnnotations){
            for (Annotation annotation:annotations){
                MyAnnotation myAnnotation = (MyAnnotation) annotation;
                String Pvalue = myAnnotation.value();
                System.out.println(Pvalue);
            }
        }
    }

}

 

6 单元测试:

单独测试某个方法或者模块。

要求:

1) 依赖JUnit jar包。

 2) 方法上要加@Test注解

 3) 方法的返回值类型为void

4) 方法是无参数的。

@Test

public void testAnnotation() throws NoSuchMethodException, SecurityException

{

User user = new User();

user.testAnno();

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值