JAVA基礎之:JAVA中的註解

1、概念

說明程序的,給計算機看的

定義:註解(Annotation),也叫元數據。一種代碼級別的說明。它是JDK1.5之後版本引入的一個特性,與類、接口、枚舉是同一個層次。它可以聲明在包、類、字段、方法、局部變量、方法參數等前面,用來對這些元素進行說明,註釋。

概念描述:

JDK1.5之後用的;

說明程序的;

使用註解:@註解名稱

package com.ma.annotation;



/**

* 註解javadoc演示

* @author mayue

* @version 1.0

* @since 1.5

*/

public class AnnoDemo01 {

    /**

     *  計算兩數之和

     * @param a 整數

     * @param b 整數

     * @return 兩數之和

     */

    public int add(int a, int b){

        return a + b;

    }

}

2、JDK中預定義的註解

    @Override

檢測被該註解的方法是否是繼承自父類(接口)的

    @Deprecated

該註解標識的內容表示已經過時

    @SuppressWarnings

壓制警告

package com.ma.annotation;



public class AnnoDemo02 {

    @Override

    public String toString(){

        return super.toString();

    }

    

    @Deprecated

    public void show1(){

        // 有缺陷

    }

    

    @SuppressWarnings("all")

    public void show2(){

        // 替代show1()

    }

    public void demo(){

        show1();

    }

}

3、自定義註解

     格式:元註解+public @interface XXX {}

        元註解(描述註解的註解)            1、@Target:描述註解可以作用的位置


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

}

            2、@Retention:描述註解保留的階段(源碼、class、運行時)

public enum RetentionPolicy {

    /**

     * Annotations are to be discarded by the compiler.

     */

    SOURCE,





    /**

     * 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

}

            3、@Documented:描述註解是否被抽取到api文檔中

            4、@Inherited:描述註解是否被子類繼承

        

    本質:(一個接口)

        public interface MyAnno extends java.lang.annotation.Annotation{}

    屬性:接口的抽象方法

 

4、在程序中使用註解

public @interface MyAnno1 {

    int age();

    String name() default "張三";

    // Person per();

    // MyAnno2 anno2();

    // String[] str();

}

 

        要求:

            1.屬性的返回值類型(基本數據類型、字符串、枚舉、註解、以上類型的數組)

            2.定義了屬性得賦值(可 default 默認賦值)

package com.ma.annotation;


@MyAnno1(age = 19)

public class Worker {

}

如果只有一個屬性需要賦值,並且名叫 value 賦值可省略名稱

 

實戰:

定義註解:

package com.ma.annotation;


import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;


/**

* 描述需要執行的類名和方法名

*/

@Target(value = ElementType.TYPE)

@Retention(value = RetentionPolicy.RUNTIME)

public @interface Run {

    String className();

    String methodName();

}

 

 

應用註解:

package com.ma.annotation;


@Run(className = "com.ma.annotationTest.AnnoDemo01",methodName = "show")

public class ReflectTest {

    public static void main(String[] args) {

        // 獲取類的字節碼文件

        Class<ReflectTest> reflectTestClass = ReflectTest.class;

        // 獲取上面註解對象

        // 該方法其實就是在內存中生成了一個該註解接口的子類對象

        Run run = reflectTestClass.getAnnotation(Run.class);

        // 調用註解對象中定義的抽象方法,獲取返回值

        String className = run.className();

        System.out.println(className);

        String methodNmae = run.methodName();

        System.out.println(methodNmae);

    }

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章