Java注解

参考视频:https://www.bilibili.com/video/BV114411W7id?p=53&spm_id_from=pageDriver&vd_source=cb0d3da7202ac90dd1d32ce6960fb3b7

Java注解

要给一个类增强一些功能,继承、实现一个接口,还可以使用注解

可以通过使用注解增强类、方法、属性的功能

 

1.内置注解

如:

@Override:可以确保重写的方法的确坐在于父类/接口中,可以有效避免单词拼错等情况
@Deprecated:用于提示,该方法由于安全、性能问题等,已经不推荐使用了。此外,在版本升级时,如果要计划删除一些方法,也通常会在前一个版本中将该方法加上@Deprecated,然后再在够细版本中删除。
@SuppressWarnings(value="unchecked")//压制警告(虽然可以使用SuppressWarnings压制警告,但不建议使用。)values的值:unchecked,deprecation(忽略一些过期的API),unused(是否未被使用),fallthrough(swtich是否一致往下执行,而没有break),path(忽略对路径不存在的检查),serialversionUID(忽略一个类可以序列化,但却没有序列化的警告),all(忽略一切警告)

 

2.自定义注解

自定义注解如何使用?

答:结合反射使用。

注解+反射什么时候会真正使用?

答:开发框架时。

3.元注解(补充)

元数据:修饰数据的数据

元注解:修饰注解的注解,如:@Target,@Retention,@Document,@Inherited

@Target:限制注解可以使用的位置

限制注解能够使用哪些元素上(属性、方法、类);如果一个注解没有@Target描述,则该注解可以修饰任何类型的元素;如果有@Target修饰,该注解就只能用于被@Target修饰的地方。

哪些位置?ElementType枚举

public enum ElementType {
   /** Class, interface (including annotation type), or enum declaration */
   TYPE,//类

   /** Field declaration (includes enum constants) */
   FIELD,//属性

   /** Method declaration */
   METHOD,//方法

   /** Formal parameter declaration */
   PARAMETER,//形参

   /** Constructor declaration */
   CONSTRUCTOR,//构造函数

   /** Local variable declaration */
   LOCAL_VARIABLE,//局部变量

   /** Annotation type declaration */
   ANNOTATION_TYPE,//注释类型

   /** Package declaration */
   PACKAGE,//包

   /**
    * Type parameter declaration
    *
    * @since 1.8
    */
   TYPE_PARAMETER,//标注类型参数

   /**
    * Use of a type
    *
    * @since 1.8
    */
   TYPE_USE//用于有关类型的各种方面
}

举例代码:

//自定义注解
package com.mokuiran.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(value = {ElementType.FIELD,ElementType.METHOD})
//限制运用,当用这个注解时,只能用于方法和属性
public @interface MyAnnotation {
   /*
       用定义方法的形式,定义一个属性value
       方法的名字,就是属性的名字;方法的返回值,就是属性的类型
   */
   String value() default "张三";
   int age() default 22;
}



//使用注解
package com.mokuiran.annotation;

public class Demo {
   
   @MyAnnotation(value = "李四",age = 33)
   public void test(){

  }

}

@Retention:限制注解的生命周期

public enum RetentionPolicy {
   /**
    * Annotations are to be discarded by the compiler.
    */
   SOURCE,//JVM直接将该注解丢弃

   /**
    * Annotations are to be recorded in the class file by the compiler
    * but need not be retained by the VM at run time. This is the default
    * behavior.
    */
   CLASS,//程序在编译时,会使用注解,在运行时不会使用

   /**
    * Annotations are to be recorded in the class file by the compiler and
    * retained by the VM at run time, so they may be read reflectively.
    *
    * @see java.lang.reflect.AnnotatedElement
    */
   RUNTIME//程序在编译以及运行时,都会使用注解
}

举例代码:

//自定义注解
package com.mokuiran.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


//限制运用,当用这个注解时,只能用于属性,方法和类
@Target(value = {ElementType.FIELD,ElementType.METHOD,ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
   /*
       用定义方法的形式,定义一个属性value
       方法的名字,就是属性的名字;方法的返回值,就是属性的类型
   */
   String value() default "张三";
   int age() default 22;
}








package com.mokuiran.annotation;

import java.lang.annotation.Annotation;

public class Demo {
   @MyAnnotation(value = "李四",age = 33)
   @Deprecated
   public void test() throws Exception {

       //拿到本类中的全部注解
       Annotation[] annotations = Class.forName("com.mokuiran.annotation.Demo").getMethod("test").getAnnotations();
       for(Annotation a: annotations){
           //a代表以上注解
           if (a instanceof MyAnnotation){
               System.out.println( ((MyAnnotation) a).value());
               System.out.println(((MyAnnotation) a).age());
          }else {
               System.out.println("Deprecated");
          }
      }
  }


   @SuppressWarnings("all")
   public static void main(String[] args) throws Exception {
       Demo demo = new Demo();
       demo.test();
       //输出结果如下:
       /*李四
       33
       Deprecated*/
       
  }
}
//若自定义注解时,@Retention(RetentionPolicy.ClLASS)或@Retention(RetentionPolicy.SOURCE),最终输出都为 Deprecated
//CLASS:注解只保留在.java文件,当java文件编译成class文件的时候,注解就没了

 

@Document

javadoc:java帮助文档。

默认情况下,javadoc不包含注解的解释,如果现在javadoc文档中也包含对注解的说明,则需要使用@Document标注

例如,以下MyAnnotation注解,会在生成javadoc时,被显示在文档中

@Documented
public @interface Myannotation{
   
}

 

@Inherited:继承

@Inherited
public @interface Myannotation{
   
}

@Myannotation
public class A{}

public class B extends A{}//默认情况下,B不会继承A中的注解,如果想要继承,则需要加@Inherited
 

 

 

 

 

posted @   默夔然  阅读(125)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示