注解入门

什么是注解

Annotation是从 JDK 5.0开始引人的新技术

Annotation的作用

  1. 不是程序本身,可以对程序做出解释(这一点和注释comment没什么区别)

  2. 可以被其他程序(比如:编译器等)读取

Annotation的格式

  1. 注解是以"@注释名"在代码中存在的,还可以添加一些参数值,例如:

    @SuppressWarnings(value="unchecked")
    

Annotation在哪里使用

  1. 可以附加在package,class,field,method等上面,相当于给他们添加了额外的辅助信息。我们可以通过反射机制编程实现对这些元数据的访问

代码示例

public class HelloWorld extends Object {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }

    @Override
    public String toString() {
        return super.toString();
    }
}
其中@Override就是一个重写注解

内置注解

@Override

定义在java.lang.Override中,此注释只用于修饰***方法***,表示一个方法声明打算重写超类中的另一个方法声明

官方解释:

/**
 * Indicates that a method declaration is intended to override a
 * method declaration in a supertype. 
 *
 * @author  Peter von der Ahé
 * @author  Joshua Bloch
 * @since 1.5
 */

@Deprecated

定义在java.lang.Deprecated中,此注释可用于修饰***方法,属性,类***,表示不鼓励程序员使用这样的元素,通常是因为它很危险或者存在更好的选择

官方解释:

/**
 * A program element annotated @Deprecated is one that programmers
 * are discouraged from using, typically because it is dangerous,
 * or because a better alternative exists.
 *
 * @author  Neal Gafter
 * @since 1.5
 */

@SuppressWarnings

定义在java.lang.SuppressWarnings中,用于抑制编译时的警告信息,
与前两个注释有所不同,你需要添加一个参数才可以正确使用,这些参数都是已经定义好了的,我们选择性的使用就好了,

  1. @SuppressWarnings(“all”)

  2. @SuppressWarngings(“unchecked”)

  3. @SuppressWarnings(value = {“unchecked”,“deprecation”})

官方解释:

/**
 * Indicates that the named compiler warnings should be suppressed in the
 * annotated element (and in all program elements contained in the annotated
 * element).  
 *
 * @author Josh Bloch
 * @since 1.5
 */

元注解

原注解的作用就是负责注解其他注解,Java定义了4个标准的meta-annotation类型,他们被用来提供对其他annotation类型做说明

这些类型和他们所支持的类在java.lang.annotation包中可以找到(@Target,@Retention,@Documented,@Inherited)

  1. @Target:

用于描述注解的使用范围(即被描述的注解可以用在什么地方)

官方解释:

Indicates the contexts in which an annotation type is applicable.
  1. @Rentention

表示在什么级别保存该注释信息,用于描述注解的生命周期

SOURCE<CLASS<RUNTIME

官方解释:

Indicates how long annotations with the annotated type are to be retained.  
  1. @Documented

说明该注解将被包含在javadoc中

官方解释:

Indicates that annotations with a type are to be documented by javadoc and similar tools by default. 
  1. @Inherited

说明子类可以继承父类中的该注解

官方解释:

 Indicates that an annotation type is automatically inherited.

自定义注解

使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口

分析:

  1. @interface用来声明一个注解,格式public @interface 注解名{定义内容}

  2. 其中的每一个方法实际上是声明了一个配置参数

  3. 方法的名称就是参数的名称

  4. 返回值类型就是参数的类型(返回值只能是基本类型,Class,String,enum)

  5. 可以通过default来声明参数的默认值

  6. 如果只有一个参数成员,一般参数名为value

  7. 注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值

代码示例(自定义注解及使用):

public class CustomizeAnnotationTest {
    @MyAnnotation(name="工一")
    public void test() {
        
    }
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
    String value() default "";
    String name();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值