JavaSE-23 注解

学习要点

  • 注解的概念
  • 注解分类
  • 读取注解信息

  

注解概述

1  元数据

定义

描述数据的数据。

用处

文档编制、编译器检查、代码分析等

2  Java注解

  • Annotation in JDK5.0——将元数据引入Java。
  • Java注解:Java代码里的特殊标记。它为在代码中添加用Java程序无法表达的额外信息提供了一种形式化的方法。
  • 注解和注释的区别:

注释不会被程序所读取。注释在源代码编译成class文件过程被丢弃。

注解可以在编译、类加载、运行时被读取。

注解的分类

1  内建注解

在java.lang包下提供了3种标准的注解类型,称为内建注解。分别是@Override注解,@Deprecated注解以及@SuppressWarnings注解。

@Override注解

用来标注方法,标识该方法是重写父类的某方法。

代码示例

/** 水果类 */

public class Fruit {

   public void getObjectInfo(){  //o和0的写法容易混淆

       System.out.println("执行Fruit类的getObjectInfo方法");

   }

}

 

/**苹果类*/

public class Apple extends Fruit {

   //指定以下方法必须重写父类方法

   @Override

   public void getObjectInfo() {

       System.out.println("执行了Apple重写的Fruit的getObjectInfo方法");

   }  

}

  

 @Deprecated注解

标识已过时的程序元素(包括成员方法和成员变量)。编译器不推荐使用。

 @SuppressWarnings注解

  • 用来阻止编译器警告。用来有选择的关闭编译器对类、方法和成员变量等程序元素及其子元素的警告。
  • 抑制多个警告value的写法:

@SuppressWarnings(value={"unused","unchecked"})

@SuppressWarnings({"unused","unchecked"})

  • 抑制单个警告value的写法:

@SuppressWarnings(value="unused")

@SuppressWarnings("unused")

  • 常用注解参数:

 

 

deprecation

使用了过时的程序元素

unchecked

执行了未检查的转换

unused

有程序元素未被使用

fallthrough

switch程序块直接通往下一种情况而没有使用break

path

在类路径、源文件路径等中有不存在的路径

serial

在可序列化的类上缺少serialVersionUID定义

finally

finally子句不能正常执行

all

所有情况

上机练习

需求说明

  • 定义Fruit类,包含名称属性(name)和打印的方法print()。其中name属性标识为已过时
  • 定义苹果类(Apple)继承Fruit类,使用注解限定重写print()方法
  • 在main()方法中实例化两个苹果对象,并使用注解抑制对象未使用的编译器警告信息。

2  元注解

java.lang.annotation包下提供了4个元注解,他们用来修饰其他注解定义。

注解

作用及成员变量

@Target

注解的作用目标,指定被其修饰的注解能用于修饰哪些程序元素。

成员变量value为ElementType 枚举类型

@Retention

注解的保留位置。

成员变量value:RetentionPolicy 枚举类型

@Documented

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

@Inherited

指定被其修饰的注解将具有继承性,说明子类可以继承父类中的该注解。

注:

ElementType:java.lang.annotation.ElementType

RetentionPolicy:java.lang.annotation.RetentionPolicy

示例:

@Target:注解的作用目标        
@Target(ElementType.TYPE)   //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法        
@Target(ElementType.PARAMETER) //方法参数        
@Target(ElementType.CONSTRUCTOR)  //构造函数        
@Target(ElementType.LOCAL_VARIABLE)//局部变量        
@Target(ElementType.ANNOTATION_TYPE)//注解        
@Target(ElementType.PACKAGE) ///包     

@Retention:注解的保留位置
@Retention(RetentionPolicy.SOURCE)   //注解仅存在于源码中,在class字节码文件中不包含      
@Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,      
@Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到     

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

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

   

3  自定义注解

注解类型是一种接口,但是和普通的接口不同。

定义一个新的注解类型:

public @interface 注解名称 {}

  注解也可以带成员变量

注解中以无参数方法的形式来声明成员变量,例如:

public @interface MyAnnotation {

   String typeData() default "BigData";

}

  使用default关键字为注解类型的成员变量指定初始值。

 

 

 

综合示例

/** 自定义注解 */
package com.etc.andemo;
import static java.lang.annotation.ElementType.METHOD;
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //指定该注解仅作用于方法 @Target(METHOD) // 指定该注解可使用反射读取 @Retention(RetentionPolicy.RUNTIME) public @interface ImageType { String formater() default "GIF"; }
/**应用自定义注解*/ package com.etc.andemo; import java.lang.annotation.Annotation; public class ImageHelper { @ImageType(formater = "GIF") public void checkType() throws SecurityException, NoSuchMethodException { Annotation[] anns = ImageHelper.class.getMethod("checkType") .getAnnotations(); for (Annotation ann : anns) { if (ann instanceof ImageType) { if (((ImageType) ann).formater().equals("GIF")) { System.out.println("这是一张GIF图片"); } else if (((ImageType) ann).formater().equals("PNG")) { System.out.println("这是一张PNG图片"); } else { System.out.println("其他类型图片"); } }else{ System.out.println("没有合适的注解!"); } } } } /**测试*/ package com.etc.andemo; public class Test { public static void main(String[] args) throws SecurityException, NoSuchMethodException { ImageHelper im=new ImageHelper(); im.checkType(); } }

  

上机练习

需求说明

  • 自定义注解,只能修饰方法,包含一个成员变量age,有默认值20
  • 定义Student类,包含验证年龄的方法,使用自定义注解修饰,在此方法中读取注解中的年龄信息并进行判断,如果年龄在18岁以下,提示“未成年”,否则,提示“具备入学基本条件”
  • 定义测试类


本博客文章未经许可,禁止转载和商业用途!

如有疑问,请联系: 2083967667@qq.com


原文地址:https://www.cnblogs.com/rask/p/8254210.html