java教程之java註解annotation使用方法_java

來源:互聯網
上載者:User

1.概述

註解可以定義到方法上,類上,一個註解相當與一個類,就相當於執行個體了一個對象,加上了註解,就相當於加了一個標誌。

常用的註解:
@Override:表示重新父類的方法,
這個也可以判斷是否覆蓋的父類方法,在方法前面加上此語句,如果提示的錯誤,那麼你不是覆蓋的父類的方法,要是提示的沒有錯誤,那麼就是覆蓋的父類的方法。
@SuppressWarnings("deprecation"):取消編譯器的警告(例如你使用的方法過時了)
@Deprecated:在方法的最上邊也上此語句,表示此方法過時,了,或者使用在類上面

複製代碼 代碼如下:

import java.util.ArrayList;
import java.util.List;
public class annotationDemo {
/*
* 對於集合,如果沒有指定儲存的類型,那麼就會有安全警告,
* 如果不想提示安全警告的話,那麼就所在類或者方法上添加@SuppressWarnings(參數)
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List list=new ArrayList();
}
}

2.自訂註解

1.格式
許可權 @interface 註解名稱 { }
步驟:
定義註解類--->定義應用註解類的類--->對應用註解類的類進行反射的類(這個類可以另外定義,也可以是在應用註解類中進行測試)

複製代碼 代碼如下:

import java.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
//定義此註解保留在位元組碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
@MyAnnotation
// 應用定義的註解類
public class ApplyMyAnnotation {
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的註解類
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
   }
}

2.聲明周期

格式:例如:@Retention(RetentionPolicy.CLASS)
在自定一的註解類上定義周期,@Retention(參數類型) 參數類型是RetentionPolicy
RetentionPolicy.CLASS:類檔案上,運行時虛擬機器不保留註解
RetentionPolicy.RUNTIME:類檔案上,運行時虛擬就保留註解
RetentionPolicy.SOURCE:源檔案上,丟棄註解
SuppressWarnings和Override是RetentionPolicy.SOURCE,
Deprecated是在RetentionPolicy.RUNTIME,要向運行時調用定義的一樣,那麼必須是RetentionPolicy.RUNTIME,
預設的都是RetentionPolicy.CLASS:

3.指定目標
格式:例如:方法上@Target(ElementType.METHOD)
定義的註解可以註解什麼成員。如果不聲明此註解,那麼就是可以放到任何程式的元素上。
可以是包,介面,參數,方法,局部變數,欄位…等。

複製代碼 代碼如下:

//定義此註解保留在位元組碼中
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})//可以定義在方法上和類上介面,表示類型
public @interface MyAnnotation {
}
@MyAnnotation
// 應用定義的註解類
public class ApplyMyAnnotation {
@MyAnnotation//定義在方法上
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的註解類
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}

3.為註解添加屬性
1.類型
註解的屬性置可以是:8個基礎資料型別 (Elementary Data Type),String,枚舉,註解,Class,數群組類型,
2.注意點
當注 解中只有一個屬性或者是只有一個屬性需要賦值的話,那麼在調用的時候,就可以直接寫入,不需要指定屬性名稱,
當註解的屬性是數群組類型並且賦值的時候只賦值一個值,那麼就可以省略{}.
3.樣本
3.1.屬性類型(是String)

複製代碼 代碼如下:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//定義此註解保留在位元組碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//設定預設值是"red"
}
@MyAnnotation("java")
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* 這是獲得類上的註解,也可以獲得方法上的註解,下面就以獲得類上的註解為例
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的註解類
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
}
}
  }

結果:
value=java
Color=red
從調用的程式中,也可以看出,只有一個屬性可以需要賦值的話,可以省略屬性名稱。否則@註解類(屬性名稱=值)
3.2.綜合類型

複製代碼 代碼如下:

/*枚舉類*/
public enum Week{
SUN,MON;
}
/**
* 註解類
*/
public @interface annotationText {
String value();
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//定義此註解保留在位元組碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//設定預設值是"red"
Week week() default Week.MON;//枚舉類型
int [] array() default {1,2,3};//數群組類型
annotationText annotation() default @annotationText("MY");//註解類型
Class classDemo() default Integer.class;//Class類型
}
@MyAnnotation(value="java",Color="green",week=Week.SUN,array=5,annotation=@annotationText("YOU"),classDemo=String.class)//數組array={4,5,6}
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* 這是獲得類上的註解,也可以獲得方法上的註解,下面就以獲得類上的註解為例
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的註解類
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
System.out.println("week="+annotation.week());
System.out.println("array長度="+annotation.array()。length);
System.out.println("註解類型值="+annotation.annotation()。value());
System.out.println("Class類型值="+annotation.classDemo());
}
}
}

 結果:
 

複製代碼 代碼如下:

value=java
Color=green
week=SUN
array長度=1
註解類型值=YOU
Class類型值=classjava.lang.String

4.Method上的註解

複製代碼 代碼如下:

importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
/**
*註解類
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface annotationText{
Stringvalue();
}
publicclassApplyMyAnnotation{
publicstaticvoidmain(String[]args)throwsException{
Methodmethodshow=ApplyMyAnnotation.class.getMethod("show");
annotationTextanno=methodshow.getAnnotation(annotationText.class);
System.out.println(anno.value());
}
@annotationText("java")
publicvoidshow(){
System.out.println("hello");
}
}

結果:java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.