java注解

一、jdk中常見(jiàn)注解

  • @Override :覆蓋父類的方法。
  • @Deprecated : 方法已經(jīng)過(guò)時(shí),再使用此方法時(shí)代碼上會(huì)出現(xiàn)橫線。
  • @SuppressWarnings("deprecated") : 忽略Deprecated 警告,去掉代碼上會(huì)出現(xiàn)的橫線。

二、注解分類(按照運(yùn)行機(jī)制分)

  • 1、源碼注解:注解只在源碼中存在,編譯成class文件就不存在了。
  • 2、編譯時(shí)注解:注解在源碼和class文件中都存在(@Overrid、@Deprecated、@SuppressWarnings)
  • 3、運(yùn)行時(shí)注解:在運(yùn)行階段還起作用,甚至?xí)绊戇\(yùn)行邏輯的注解
    按照來(lái)源區(qū)分
  • 1、jdk注解
  • 2、第三方注解
  • 3、自定義注解

三、自定義注解

package com.tiandy.producer;
import java.lang.annotation.*;
//自定義注解
/*
   1、使用@interface 關(guān)鍵字定義注解
   2、成員以無(wú)參無(wú)異常方式聲明
   3、可以用default為成員指定一個(gè)默認(rèn)值
   4、成員類型是受限的,合法的類型包括原始類型及String、class、Annotation、Enumeration
   5、如果注解只有一個(gè)成員,則成員名必須取名為value(),在使用時(shí)可以忽略成員名和賦值號(hào)(=)
   6、注解類可以沒(méi)有成員,沒(méi)有成員的注解稱為標(biāo)識(shí)注解
 */
//元注解,即用在注解上的注解。
//注解的作用域: CONSTRUCTOR:構(gòu)造方法聲明;FIELD:字段聲明;LOCAL_VARIABLE:局部變量聲明;METHOD:方法聲明;
// TYPE:類,接口;PARAMETER:參數(shù)聲明;PACKAGE:包聲明;
@Target({ElementType.METHOD, ElementType.TYPE})
//注解生命周期:SOURCE:只在源碼中顯示,編譯時(shí)會(huì)放棄;CLASS:編譯時(shí)會(huì)記錄到class中,運(yùn)行時(shí)忽略;RUNTIME:運(yùn)行時(shí)存在,可以通過(guò)發(fā)射讀取。
@Retention(RetentionPolicy.RUNTIME)
//允許子類繼承
@Inherited
//生成javadoc會(huì)包含注解信息
@Documented
public @interface Description {
    String desc();
    String author();
    int age() default 18;
}

四、使用自定義注解

package com.tiandy.producer;
/*
注解使用方法
@<注解名>(<成員名1>="成員值1",成員名2>="成員值2",...)
*/
public class UseDescription {
    @Description(desc="I am abc",author="zrh",age=20)
    public String abc(){
        return "abc";
    }
}

五、解析注解

通過(guò)反射獲取類、函數(shù)或成員上的運(yùn)行時(shí)注解信息,從而實(shí)現(xiàn)動(dòng)態(tài)控制程序運(yùn)行的邏輯。

public class TestDesc {

    public static void main(String[] args) {
        try {
            //使用類加載器加載類
            Class cla = Class.forName("com.tiandy.producer.UseDescription");
            //找到類上面的注解,參數(shù)為注解對(duì)應(yīng)的class名稱
            boolean flag = cla.isAnnotationPresent(Description.class);
            if(flag){
                Description d = (Description)cla.getAnnotation(Description.class);
                System.out.println("desc==="+d.desc());
                System.out.println("author===="+d.author());
            }
            //找到方法的注解
            Method[] ms = cla.getMethods();
            for (Method m :ms){
                boolean temp = m.isAnnotationPresent(Description.class);
                if (temp){
                    Description desc = (Description)m.getAnnotation(Description.class);
                    System.out.println("desc==="+desc.desc());
                    System.out.println("author===="+desc.author());
                }
            }
            //另一種解析方法
            for(Method m:ms){
                //拿到所有的注解
                Annotation[] annotations = m.getAnnotations();
                for (Annotation a:annotations){
                    if(a instanceof Description){
                        Description d = (Description) a;
                        System.out.println(d.desc());
                        System.out.println(d.author());
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容