程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java教程之java注解annotation應用辦法

java教程之java注解annotation應用辦法

編輯:關於JAVA

java教程之java注解annotation應用辦法。本站提示廣大學習愛好者:(java教程之java注解annotation應用辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java教程之java注解annotation應用辦法正文


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個根本數據類型,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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved