当前位置: 移动技术网 > IT编程>开发语言>Java > 注解的定义和使用

注解的定义和使用

2020年04月21日  | 移动技术网IT编程  | 我要评论

注解也叫元数据,一种代码级别的说明。是jdk1.5后产生的一个特性,与类、接口、枚举同一个档次,他可以在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明、注释

<!--more-->

注解分类

  1. 编写文档:通过代码里的标识的注解生成文档(生成word文档)

  2. 编译检查:通过代码里的标识的注解让编译器能够实现基本的编译检查

  3. 代码分析:通过代码里标识的注解对的代码进行分析(使用反射) 【重点】

注解可以在类、变量、方法、接口上使用

注解也有生命周期

一个方法上、一个变量上、一个类上,可以使用多个注解

jdk中内置的注解

  • @deprecated:用来修饰已经过时的方法。编译期注解,提示该方法或者该成员变量已经过期,不建议使用,但是还是能使用的,会在使用到该方法 或者该变量会有个横线,能用到(类、变量、方法)

  • @override:用来修饰此方法重写了父类的方法 只能在方法上

  • @suppresswarnings:用来通知java编译器禁止特定的编译警告

    @suppresswarnings("unused")

     

自定义注解

通过@interface 关键字进行定义

public @interface 注解名称{

}

使用注解、在类上使用的列子

@注解名称
public class test {

}

元注解

元注解是可以注解到注解上的注解,或者说元注解是一种基本注解,但是它能够应用到其他的注解上面。

元注解有@retention、@documented、@target、@inherited、@repeatable这5个

两个类型一样的注解不能同时用

@retention【重点】

retention 意思是保留期的意思,当@retention应用到一个注解上的时候,它解释说明了这个注解的存活时间

取值如下:

描述
retentionpolicy.source 注解只在源代码阶段博保留,在编译器进行编译时将会被丢弃忽视
retentionpolicy.class 只被保留在编译进行的时候,并不会加载到jvm中
retentionpolicy.runtime 可以保留到程序运行时,它会被加载到jvm中,所以可以获取到他们

retentionpolicy是一个枚举类

代码演示:

@retention(retentionpolicy.runtime)//定义一个运行期有效的注解
public @interface myannotation {
}

@target【重点】

目标的意思,@target指定注解运用的地方

  • elementtype.annotation_type:可以给注解进行注释

  • elementtype.constructor:可以给构造方法注解

  • elementtype.field:可以给属性进行注解

  • elementtype.local_variable:可以给局部变量进行注解

  • elementtype.method:可以给方法进行注解不能用在构造方法上

  • elementtype.package:可以给包进行注解

  • elementtype.parameter:可以给一个方法内的参数进行注解

  • elementtype.type;可以给一个类型进行注解,比如类、接口、枚举

代码实例:

//该注解可在 类 接口、枚举、方法、成员变量使用
@target({elementtype.type,elementtype.field,elementtype.method})
@myannotation
public class student {
   @myannotation
   int age;
   @myannotation
   public void test() {
  }
}

@inherited

表示该注解能被子类使用(继承)

注解myannotation被@inherited修饰,如果有个类a使用了@myannotation注解,又有一个子类b继承了a,在b中也能使用@myannotation注解

@documented

这个注解和文档有关,它能将注解中的元素包含到javadoc中

@repeatable

在需要对同一个注解多次使用在一个类、接口、方法、属性等上面的时候,需要用@repeatable

注解的属性

注解的属性也叫成员变量,注解中只有成员变量,无成员方法

注解的成员变量在注解的定义以无参数的方法的形式来声明。其方法名定义了该成员变量的名字,其返回值定义了该成员变量的类型

@target({elementtype.type,elementtype.field,elementtype.method})
public @interface myannotation {
   //注解中只有成员变量,无成员方法
   int id() default 0;
   string username() default " ";
}

这里的default给该变量id和username设置了默认值

在student类中用了该注解的方法可以给该注解中的变量赋值,如果注解没有设置默认值,则在引用该注解的时候必须给该注解赋值

public class student {
   @myannotation(id = 12, username ="小飞龙" )
   int age;
   @myannotation
   public void test01() {
  }
   @myannotation(id = 55)
   public void test02() {
  }

}

当注解中只有一个变量的时候(也就是只有一个无参方法的时候)可以这样写

注解中的代码:

public @interface myannotation {
    //注解中只有成员变量,无成员方法
    string value() default " ";
}

student代码:

public class student {
    @myannotation("张三")
    public void test01() {
    }
  //  @myannotation(value = "李四")等价    @myannotation("张三")
  @myannotation(value = "李四")
    public void test02() {
    }
}

在这里@myannotation(value = "李四")等价    @myannotation("张三"),都是给value赋值

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网