Java———Annotation注解(一)*

简介

  1. 这不是注释
  2. 从JDK5才开始支持Annotation注解机制
  3. 访问处理Annotation的工具统称APT
  4. 一种是给程序用的注解,基本注解
  5. 一种是给注解用的注解

基本Annotation

  1. @Override 标记重写父类方法
  2. @Deprecated 标记已过时
  3. @SuppressWarnings 抑制警告信息
  4. @SafeVarags 抑制“堆污染”警告信息
  5. @FunctionalInterface 标记为函数式接口

@Override

强制指定这个方法重写父类方法,如果这个方法不是父类方法,会报编译错误。
这样做的目的是为了避免程序员重写时犯低级错误。

public class A {
    public void myo() {

    }
}

class B extends A {
    @Override
    public void myo() {

    }

    /**
     * 这里会报编译错误,那是个零
     */
    @Override
    public void my0() {

    }
}

@Deprecated

标示这个方法、类已经过时了,当其他程序使用这个过时的方法、类就会给出警告。

标注过时

@SuppressWarnings(value={String})

抑制编译警告信息。
如果抑制了这个类,就抑制了这个类内的所有方法,抑制了方法,就抑制了这个方法内的每行代码

抑制警告

抑制警告关键字

关键字用途
all抑制所有警告
boxingto suppress warnings relative to boxing/unboxing operations
castto suppress warnings relative to cast operations
dep-annto suppress warnings relative to deprecated annotation
deprecationto suppress warnings relative to deprecation
fallthroughto suppress warnings relative to missing breaks in switch statements
finallyto suppress warnings relative to finally block that don’t return
hidingto suppress warnings relative to locals that hide variable
incomplete-switchto suppress warnings relative to missing entries in a switch statement (enum case)
nlsto suppress warnings relative to non-nls string literals
nullto suppress warnings relative to null analysis
rawtypesto suppress warnings relative to un-specific types when using generics on class params
restrictionto suppress warnings relative to usage of discouraged or forbidden references
serialto suppress warnings relative to missing serialVersionUID field for a serializable class
static-accessto suppress warnings relative to incorrect static access
synthetic-accessto suppress warnings relative to unoptimized access from inner classes
uncheckedto suppress warnings relative to unchecked operations
unqualified-field-accessto suppress warnings relative to field access unqualified
unused抑制未使用警告

@SafeVarags

这个注解是Java7以后才支持的

什么是堆污染?

“堆污染”即是将一个不带泛型的变量赋值给一个带泛型的变量,将导致泛型变量污染,如果不加上面注解,编译器将给于提示,避免运行时异常。

代码

import java.util.ArrayList;
public class A {
    public static void main(String[] args) {
        ArrayList arr = new ArrayList<Integer>();
        arr.add(20);
        ArrayList<String> arrS = arr;
        System.out.println(arrS.get(0));
    }
}

消除这类警告办法

  • @SafeVarags
  • @SuppressWarnings(“unchecked”)
  • 编译时使用-Xlint:varargs选项(博主没实践过,听别人说的,有兴趣的可以试试)

@FunctionalInterface

  • Java8以后才支持的
  • 只支持接口修饰
  • 定义这个接口是函数式接口
  • 函数式接口:只有一个抽象方法,可以后多个默认方法或多个static方法的接口
  • 作用:如果这个接口不是函数式接口,就会报编译错误,帮助程序员纠错
  • 出现目的:为了Lambda表达式准备的

JDK的元Annotation

用于修饰注解的注解

  1. @Retention 保留时间
  2. @Target 作用对象
  3. @Documented
  4. @Inherited

@Retention(value)

用于指定被修饰的Annotation可以保留多长时间

value值含义
RetentionPolicy.CLASS编译器把Annotation记录在class文件中。当Java程序运行时,JVM不能获取Annotation信息。这是默认值
RetentionPolicy.RUNTIME编译器把Annotation记录在class文件中。当Java程序运行时,JVM获取Annotation信息,也能通过反射获取Annotation信息。这是保留到运行时
RetentionPolicy.SOURCEAnnotation保留到源代码中,编译器直接丢弃

@Target(value)

用于指定被修饰的Annotation能用于程序的那些对象

value值含义
ElementType.ANNOTATION_TYPE该Annotation能修饰Annotation
ElementType.CONSTRUCTOR该Annotation能修饰构造器
ElementType.FIELD该Annotation能修饰成员变量
ElementType.LOCAL_VARIABLE该Annotation能修饰局部变量
ElementType.METHOD该Annotation能修饰方法定义
ElementType.PACKAGE该Annotation能修饰包定义
ElementType.PARAMETER该Annotation能修饰参数
ElementType.TYPE该Annotation能修饰类、接口、注解类接口、枚举
ElementType.PARAMETERJava8
ElementType.USEJava8

@Documented

用于指定被修饰的Annotation能被javadoc工具提取成文档。
就是提取API的时候,Documented也能提取出来。

@Inherited

用于指定被修饰的Annotation具有继承。
就是B继承A,如果A添加Annotation修饰,那么B也有相同的Annotation修饰,前提是这个Annotation具有继承性。

Java8新增注解

@Repeatable

重复注解

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值