java注解学习(1)注解的作用和三个常用java内置注解

   今天,记录一下自己学习的关于注解方面的知识。

Annotation是从JDK5.0开始引入的新技术

Annotation的作用

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

-可以被其他程序(比如编译器)读取。(注解信息处理流程,是注解和注释的重大区别,如果没有注解信息处理流程,则注解将毫无意义。)

-注解是以 "@注释名" 在代码中存在的,还可以添加一些数值,如: @SuppressWarnings(value="unchecked")。

Annotation在哪里使用:

-可以在package,class,method,field等上面,就如同给它们添加了额外的辅助信息,后面我们可以通过反射机制编程实现对这些元数据的访问。

常用的java内置注解:

1.@Override

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

package java.lang;

import java.lang.annotation.*;

/**
 * Indicates that a method declaration is intended to override a
 * method declaration in a supertype. If a method is annotated with
 * this annotation type compilers are required to generate an error
 * message unless at least one of the following conditions hold:
 *这段话指明了Overiide的作用,说明Override用来暗示这是一个重写父类的一个方法,如果这个方法没有重写父类的方法编译器就会生成一个错误
* <ul><li> * The method does override or implement a method declared in a * supertype. * </li><li> * The method has a signature that is override-equivalent to that of * any public method declared in {
@linkplain Object}. * </li></ul> * * @author Peter von der Ah&eacute; * @author Joshua Bloch * @jls 9.6.1.4 @Override * @since 1.5 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.SOURCE) public @interface Override { }

 @interface是定义注解的关键字,@Target和@Retention也是注解,一个表示定义的注解可以修饰在哪,一个表示定义的注解的生命周期。这里我们知道就好后面再细说。

@Overiide就是用来说明修饰的方法是一个重写父类的方法,如果没有重写父类的方法就会报错。

public class TestAnnotation01 /* extends Object */{
  //TestAnnotation01 继承了Object类,这里我们使用注解@Override重写了Object方法,如果我们修改一下toString的名字,编译器就会报错
 @Override
  public String toString(){
      return "";
  }
}

2. @Deprecated

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

package java.lang;

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;

/**
 * A program element annotated &#64;Deprecated is one that programmers
 * are discouraged from using, typically because it is dangerous,
 * or because a better alternative exists.  Compilers warn when a
 * deprecated program element is used or overridden in non-deprecated code.
 * 带注释的程序元素是不鼓励程序员使用的,通常是因为它很危险,或者因为存在更好的替代方法。
 * @author  Neal Gafter
 * @since 1.5
 * @jls 9.6.3.6 @Deprecated
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}

当然我们硬要使用这个方法也是可以的

3.@SuppressWarnings

-定义在java.lang.SupressWarnings中,用来抑制编译时的警告信息

-与前面两个注解有所不同,需要添加一个参数才能正确使用,这些参数都是定义好的

参数如下:

参数含义
deprecation 使用了过时的类或方法时的警告
unchecked 执行了未检查的转换时的警告
fallthrough 当Switch程序块进入进入下一个case而没有Break时的警告
path 在类路径、源文件路径等有不存在路径时的警告
serial 当可序列化的类缺少serialVersionUID定义时的警告
finally 任意finally子句不能正常完成时的警告
all 以上所有情况的警告
package java.lang;

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;

/**
 * Indicates that the named compiler warnings should be suppressed in the
 * annotated element (and in all program elements contained in the annotated
 * element).  Note that the set of warnings suppressed in a given element is
 * a superset of the warnings suppressed in all containing elements.  For
 * example, if you annotate a class to suppress one warning and annotate a
 * method to suppress another, both warnings will be suppressed in the method.
 * 指示指定的编译器警告应该在带注释的元素(以及带注释的元素中包含的所有程序元素)中被抑制。
注意,在给定元素中抑制的警告集是所有包含元素中抑制的警告的超集。
例如,如果注释一个类来抑制一个警告,注释一个方法来抑制另一个警告,那么这两个警告都会在方法中被抑制。 * <p>As a matter of style, programmers should always use this annotation * on the most deeply nested element where it is effective. If you want to * suppress a warning in a particular method, you should annotate that * method rather than its class. * *
@author Josh Bloch * @since 1.5 * @jls 4.8 Raw Types * @jls 4.12.2 Variables of Reference Type * @jls 5.1.9 Unchecked Conversion * @jls 5.5.2 Checked Casts and Unchecked Casts * @jls 9.6.3.5 @SuppressWarnings */ @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.SOURCE) public @interface SuppressWarnings { /** * The set of warnings that are to be suppressed by the compiler in the * annotated element. Duplicate names are permitted. The second and * successive occurrences of a name are ignored. The presence of * unrecognized warning names is <i>not</i> an error: Compilers must * ignore any warning names they do not recognize. They are, however, * free to emit a warning if an annotation contains an unrecognized * warning name. * * <p> The string {@code "unchecked"} is used to suppress * unchecked warnings. Compiler vendors should document the * additional warning names they support in conjunction with this * annotation type. They are encouraged to cooperate to ensure * that the same names work across multiple compilers. * @return the set of warnings to be suppressed */ String[] value(); }

例如下面

我们在创建List对象没指定存储的对象类型就会警告,现在我们给它加上@SupressWarnings注解。

警告就被抑制了,当然你把@SupressWarnings加在类上面也是可以的。

总结:

  以上就是java Annotation的基础内容和三个常用的java内置注解,后面我们开始学习如何自定义注解。

原文地址:https://www.cnblogs.com/fankailei/p/9846136.html