天天看點

JAVA EE(九)—— 注解(Annotation)一、注解二、注解示例

文章目錄

  • 一、注解
    • 1、注解概述
    • 2、常見注解
  • 二、注解示例
    • 1、注解說明
    • 2、注解示例

一、注解

1、注解概述

(1)注解介紹

  • 注解(Annotation)不是程式本身,可以對程式作出解釋說明
  • 注解可以被其他程式(比如編譯器等)讀取(注解資訊處理流程,是注解和注釋的重大差別)。

(2)注解的文法

@注釋名
後面還可以添加一些參數,比如
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
           
  • @符号的作用:标示這是一個注解。

(3)注解的使用

可以在package、class、method、field等上面使用,相當于給它們添加了額外的資訊。

2、常見注解

(1)

@Override

重寫注解

定義在

java.lang.Override

中,此注釋隻适用于修飾方法,表示一個方法聲明打算重寫其他類中的另一個方法聲明。

(2)

@Deprecated

過時注解

定義在

java.lang.Deprecated

中,此注釋可用于修飾方法、屬性、類,表示不建議使用被注釋内容,通常是因為這些内容過時或者很危險或者存在更好的選擇。

(3)

@SuppressWarnings

警告注解

定義在

java.lang.SuppressWarnings

中,用來抑制編譯時的警告資訊。這個注釋必須要添加參數才能使用,這些參數都是已經定義好了的,可以直接使用

參數如:

  • deprecation

    :使用了過時的類或者方法的警告;
  • unchecked

    :執行了未檢查的轉換的警告,如使用集合時未指定泛型;
  • path

    :在類路徑、源檔案路徑等中有不存在路徑的警告;
  • fallthrough

    :當在switch語句使用發生case穿透;
  • serial

    :當在可序列化的類上缺少 serialVersionUID 定義是的警告
  • finally

    :任何finally子句中不能完成時的警告;
  • all

    :關于以上所有情況的警告。
//例如
@SuppressWarnings("unchecked")
           

二、注解示例

1、注解說明

(1)@interface

使用@interface自定義注解時,自動繼承了

java.lang.annotation.Annotation

接口

(2)元注解(meta-annotation)

① @Target:用于描述注解的使用範圍,即被描述的注解可以使用在什麼地方。

  • PACKAGE:用于描述包
  • TYPE:用于類、接口、枚舉、Annotation類型
  • CONSTRUCCTOR:用于描述構造器
  • FIELD:用于描述字段(域)
  • METHOD:用于描述方法
  • LOCAL_VARIABLE:用于描述局部變量
  • PARAMETER:用于描述參數
例如
@Target(value=ElementType.TYPE)
           

② @Retention:表示需要在什麼級别儲存該注釋資訊,用于描述注解的生命周期。

-SOURCE:在源檔案中有效(即源檔案保留)

  • CLASS:在class檔案中有效(即class保留)
  • RUNTIME:在運作時有效(即運作時保留),為Runtime可以被反射機制讀取

③ @Documented:

④ @Inherited:

元注解的作用是負責注解其他注解

2、注解示例

(1)示例1:在類上面自定義注解

① 自定義注解接口類

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation {
	public String name();
	public String value();
}
           

@Retention(RetentionPolicy.RUNTIME)

設定注解在運作時有效

@Target(ElementType.TYPE)

設定注解類型,表示是在類上面注解還是方法上注解還是其他上面注解等。

② 定義一個普通類

@MyAnnotation(name = "name",value = "value")
public class ClassDemo {
	
}
           

③ 主函數類

public class TestDemo {
	public static void main(String[] args) {
		Class classDemo = ClassDemo.class;
		Annotation anno = classDemo.getAnnotation(MyAnnotation.class);
		if(anno instanceof MyAnnotation) {
			MyAnnotation myAnnotation = (MyAnnotation)anno;
			System.out.println(myAnnotation.name() + " " + myAnnotation.value());
		}
	}
}
           

(2)示例2:在方法上注解

① 自定義注解接口類

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
	public String name();
	public String value();
}
           

② 定義一個普通類

public class ClassDemo {
	@MyAnnotation(name = "hello",value = "world")
	public void eat(){
		
	}
}
           

③ 主函數類

public class TestDemo {
	public static void main(String[] args) throws NoSuchMethodException, SecurityException {
		Class classDemo = ClassDemo.class;
		Method method = classDemo.getMethod("eat", null);
		MyAnnotation anno = method.getAnnotation(MyAnnotation.class);
		if(anno instanceof MyAnnotation) {
			MyAnnotation myAnnotation = (MyAnnotation)anno;
			System.out.println(myAnnotation.name() + " " + myAnnotation.value());
		}
	}
}
           

(3)示例3:對參數的注解

① 自定義注解類

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Demo1 {
	public String value();

}
           

② 定義一個普通類

public class FromDemo1 {
	public void methodDemo(@Demo1("張三")String value) {
			
	}
}
           

③ main函數類

public class TestDemo1 {
	public static void main(String[] args) throws NoSuchMethodException, SecurityException {
		Class clazz = FromDemo1.class;
		//擷取方法對象
		Method method = clazz.getMethod("methodDemo", String.class);
		//使用method對象擷取Parameter的注解
		Annotation[][] annotations = method.getParameterAnnotations();
		
		for(Annotation[] anno: annotations) {
			for(Annotation a: anno) {
				//判斷
				if(a instanceof Demo1) {
					Demo1 demo1 = (Demo1)a;
					System.out.println(demo1.value());
				}
			}
		}
	}
}
           

(4)示例4:對字段的注解

① 自定義注解類

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Demo2 {
	public String name() default "李四";
}
           

② 普通類

public class FromDemo2 {
	@Demo2()
	public String name = "李四";
}
           

③ main函數類

public class TestDemo2 {
	public static void main(String[] args) throws NoSuchFieldException, SecurityException {
		Class<FromDemo2> clazz = FromDemo2.class;
		Field field = clazz.getField("name");
		Annotation[] annotations = field.getAnnotations();
		for(Annotation anno: annotations) {
			if(anno instanceof Demo2) {
				Demo2 demo2 = (Demo2)anno;
				System.out.println(demo2.name());
			}
		}
	}
}