[javaSE] 註解-自訂註解,javase註解自訂

來源:互聯網
上載者:User

[javaSE] 註解-自訂註解,javase註解自訂

註解的分類:

源碼註解

編譯時間註解 JDK的@Override

運行時註解 Spring的@Autowired

 

自訂註解的文法要求

① 使用@interface關鍵字定義註解

② 成員以無參無異常方式執行

③ 可以用defaut為成員指定一個預設值

④ 成員類型是受限的包括原始類型,及String Class Annotation Enumberation

⑤ 如果註解只有一個成員,則成員名必須取名為value(),在使用時可以忽略成員名和賦值號(=)

⑥ 註解類可以沒有成員,稱為標識註解

package com.tsh.ano;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;//元註解@Target({ElementType.METHOD,ElementType.TYPE})//範圍@Retention(RetentionPolicy.RUNTIME)//生命週期@Inherited//可以被繼承@Documented//產生文檔public @interface Description {    String desc();    String author();    int age() default 18;}//使用註解class DescAnoDemo{    @Description(author = "taoshihan", desc = "hahaha")    void show(){            }}

 

元註解:註解的註解

使用自訂註解

解析註解

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.