最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Java自定義注解相關(guān)知識

 更新時(shí)間:2021年05月25日 14:44:48   作者:韓曙亮  
今天帶大家來學(xué)習(xí)Java注解的相關(guān)知識,文中對自定義注解作了非常詳細(xì)的介紹,對正在學(xué)習(xí)Java的小伙伴們很有幫助,需要的朋友可以參考下

一、自定義注解格式

分析 Java 中自帶的 @Override 注解 , 源碼如下 :

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

注解分為兩部分 :

① 元注解 ;

② public @interface 注解名稱 ;

二、注解本質(zhì)分析

按照 public @interface 注解名稱 格式 , 寫出一個(gè)注解 , 編譯該注解代碼生成 Annotation.class 字節(jié)碼文件 ;

public @interface Annotation {
}

使用 javap 命令反編譯Annotation.class 字節(jié)碼文件 , 查看該注解的實(shí)際代碼 ;

反編譯命令如下 :

javap Annotation.class

輸出內(nèi)容 :

D:\002_Project\004_Java_Learn\Annotation\out\production\Annotation>javap Annotation.class
Compiled from "Annotation.java"
public interface Annotation extends java.lang.annotation.Annotation {
}

在這里插入圖片描述

注解的本質(zhì)是一個(gè) interface 接口 , 注解接口默認(rèn)繼承了 java.lang.annotation.Annotation 接口 ;

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

三、注解屬性及類型

注解的本質(zhì)是接口 , 接口中可以定義 常量 和 方法 ;

在注解中定義 接口方法 , 就是 注解的屬性 ;

為注解添加屬性 : 接口中的方法都是抽象方法 , 其中 public abstract 可以省略 ;

public @interface Annotation {
    public abstract String path();
}

注解屬性使用格式 :

@注解名稱(屬性名稱 = 屬性值)

注解屬性使用 : 在相關(guān)的代碼上使用

 @Annotation(path = "")
    Student(String name, int age){
    }

四、注解屬性類型

注解屬性 ( 接口方法 ) 返回值類型要求 :

① 基本數(shù)據(jù)類型 : byte , short , int , long , float , double , char , boolean ;

② 字符串類型 : String ;

③ 枚舉類型 : enum ;

④ 注解類型 ;

⑤ 以上類型的數(shù)組形式 ;

注解屬性返回值必須是以上的類型 , 不能設(shè)置其它類型返回值 , 否則會報(bào)錯(cuò) ;

注解中定義了屬性 , 在使用注解時(shí) , 需要 給 注解屬性 賦值 ;

定義 注解屬性 時(shí) , 可以 使用 default 關(guān)鍵字 指定屬性默認(rèn)值 , 下面代碼中 , 制定 注解屬性 intValue 值類型為 int 整型 , 默認(rèn)值 88 ;

int intValue() default 88;

如果 注解屬性 指定了默認(rèn)值 , 在使用注解時(shí) , 可以選擇 不為該屬性賦值 ( 此時(shí)使用默認(rèn)屬性值 ) , 也可以進(jìn)行賦值 ( 指定一個(gè)新的屬性值 ) ;

如果 注解屬性 沒有指定默認(rèn)值 , 則使用 注解 時(shí) , 必須為其指定一個(gè)默認(rèn)值 , 否則編譯時(shí)報(bào)錯(cuò) ;

數(shù)組類型 的 注解屬性 賦值 時(shí) , 使用大括號進(jìn)行賦值 , 大括號內(nèi)是數(shù)組元素 , 如果只有一個(gè)屬性 , 可以省略大括號 ,

注解 聲明示例 :

public @interface Annotation {
    /**
     * 字符串類型
     * @return
     */
    String stringValue();

    /**
     * int 基本類型
     * @return
     */
    int intValue() default 88;

    /**
     * 枚舉類型
     * @return
     */
    Number enumValue();

    /**
     * 注解類型
     * @return
     */
    Annotation2 annotationValue();

    /**
     * 字符串?dāng)?shù)組類型
     * @return
     */
    String[] stringArrayValue();
}

枚舉類 :

public enum Number {
    ONE, TWO, THREE
}

Annotation2 注解類 :

public @interface Annotation2 {
}

注解使用示例 :

/**
 * 注解生成文檔
 *
 * @author hsl
 * @version  0.1
 * @since 1.5
 */
public class Student {
    /**
     * 構(gòu)造函數(shù)
     * @param name 參數(shù)一
     * @param age 參數(shù)二
     */
    @Annotation(
            stringValue = "tom",
            enumValue = Number.ONE,
            annotationValue = @Annotation2,
            stringArrayValue = {"tom", "jerry"})
    Student(String name, int age){
    }

    @SuppressWarnings("all")
    @Override
    public String toString() {
        return super.toString();
    }
}

代碼分析 : 重點(diǎn)關(guān)注注解的使用 , 使用注解時(shí) , 需要給 沒有默認(rèn)值 的 注解屬性 賦值 , 格式為 注解屬性名稱 = 對應(yīng)類型屬性值 , 如果 注解屬性 有默認(rèn)值 , 則

@Annotation(stringValue = "tom", enumValue = Number.ONE, stringArrayValue = {"tom", "jerry"})

五、注解屬性賦值簡化操作

如果 注解屬性 名稱是 value , 并且 注解中只有 1 1 1 個(gè)屬性 , 那么在使用 注解 為 注解屬性 賦值時(shí) , 可以省略注解名稱 , 直接傳入 注解屬性值 ;

示例 : JDK 自帶的 SuppressWarnings 注解 ,

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    String[] value();
}

注解使用 : 使用 SuppressWarnings 注解時(shí) , 直接傳入 “all” 參數(shù) , 省略了注解屬性名稱 ;

 @SuppressWarnings("all")
    @Override
    public String toString() {
        return super.toString();
    }

滿足兩個(gè)條件 , 才能使用上述簡化方式 ;

到此這篇關(guān)于淺談Java自定義注解相關(guān)知識的文章就介紹到這了,更多相關(guān)Java自定義注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

西安市| 阿瓦提县| 星子县| 原平市| 柘城县| 泸水县| 洛隆县| 花莲县| 手机| 中阳县| 桦南县| 澳门| 河池市| 昭通市| 湘潭市| 东乌珠穆沁旗| 长海县| 府谷县| 白河县| 云南省| 湖北省| 克什克腾旗| 玉屏| 鄂州市| 务川| 怀宁县| 杂多县| 陆丰市| 新安县| 平定县| 邹城市| 唐海县| 遂溪县| 荃湾区| 信阳市| 临潭县| 鹰潭市| 灵川县| 华蓥市| 青海省| 贺兰县|