注解

 Java注解

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

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

       内置注解:

               @Override :可以确保 重写的方法 的确存在于父类/接口中,可以有效的避免 单纯拼错等情况。

            @Deprecated:给用于提示,该方法由于安全,性能问题等,已经不推荐使用了 此外,在版本升级时,

                                          要计划删除一些方法,也通常会在前一个版本中

                                    将该方法加上@Deprecated,然后再后续版本中删除。

           @SuppressWarnings(value="unchecked") :压制警告(不建议使用) 忽略对泛型等的检查操作。

                                   value值:unchecked,deprecation(忽略一些过期的API),

                                                      unused(是否未被使用),fallthrough(swtich是否一致往下 执行,而没有break),

                                                 path(忽略 对类路径不存在的检查),serialversionUID(忽略 一个类可以序列化,但却没有序列化的行为的警告),

                                                  all忽略以上所有情况。

package annontaion;

import annontaion.Fathe;

class Fathe
{
   public void eat(){
       System.out.println("father eat...");
  }
}
class Son extends Fathe{
   @Override  //一般重写父类方法或实现类实现接口的方法时候加入注解 :内置注解
   public void eat(){
       System.out.println("son eat....");
  }
}
public class Demo01
{
   public static void main(String[] args) {
       Fathe f=new Son();
       f.eat();
       /*
       son eat....
        */
  }
}

 

       自定义注解: public @interface MyAnnotation { }

                  元注解:

                          元数据:修饰数据的数据

                          元注解:修饰注解的注解: @Targe,@Retention,@Document,@lnherited

   @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 annontaion;

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,ElementType.PARAMETER})
public @interface MyAnnotation {
   /*
   用定义方法的形式,定义一个属性 value
   方法的名字,就是属性的名字;方法的返回值,就是属性的类型
    */
   String value() default "zhangs";//String value="zhangs"
   int age() default 22;
}
package annontaion;

public class TestMyAnnotaiton
{
   @MyAnnotation(value = "lisi",age=33)//使用注解
public void test()
{
}
}

 

      自定义注解如何使用?结合反射使用。

       注解+反射 什么时候会真正使用?开发框架时 Spring mybatis springmvc

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

使用注解案例:

package annontaion;

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,ElementType.PARAMETER,ElementType.LOCAL_VARIABLE})
        @Retention(RetentionPolicy.RUNTIME)   //整个生命周期
public @interface MyAnnotation {
   /*
   用定义方法的形式,定义一个属性 value
   方法的名字,就是属性的名字;方法的返回值,就是属性的类型
    */
   String value() default "zhangs";//String value="zhangs"
   int age() default 22;
}
package annontaion;

import java.lang.annotation.Annotation;

//使用注解
public class TestMyAnnotation
{
   @MyAnnotation(value = "李四" ,age = 33)
   @Deprecated
   public static void test() throws Exception {
       /*
       注解在这个类里面的 TestMyAnnotation
       因此先通过类拿到方法前面的那个注解 @MyAnnotation
        */
       Annotation[] annotations = Class.forName("annontaion.TestMyAnnotation").getMethod("test").getAnnotations();//通过反射可以拿到一切
/*
       拿到这个类里面这个方法的前面的注解
        */
       //因为有两个注解了,所以要遍历
       for (Annotation a:annotations)
      {
           if (a instanceof MyAnnotation){
               System.out.println(((MyAnnotation)a).value()); //强转
               System.out.println(((MyAnnotation)a).age());
          }else {//@Deprecated 不推荐的注解
               System.out.println("@Deprecated");
          }
      }
  }
   @SuppressWarnings("all")
   public static void main(String[] args) throws Exception {
       test();
       /*
       李四
       33
       @Deprecated
       */
  }
}

 

          @Document:

                           javadoc:java帮助文档。ABC.java-->帮助文档 默认情况下,javadoc不包含 注解的解释;

                                              如果现在javadoc文档中 也包含对注解的说明,则需要使用@Document标注

 

         @lnherited:继承

@Inherited
 public @interface MyAnnotation{
}
@MyAnnotation
public class A{}

public class B extends A{}//默认情况下,B不会继承A中的注解;如果要想继承,则需要加 @lnherited
 
posted @ 2022-09-28 22:33  zjw_rp  阅读(91)  评论(0)    收藏  举报