java教程之java注解annotation使用方法
1.概述
注解可以定義到方法上,類上,一個(gè)注解相當(dāng)與一個(gè)類,就相當(dāng)于實(shí)例了一個(gè)對象,加上了注解,就相當(dāng)于加了一個(gè)標(biāo)志。
常用的注解:
@Override:表示重新父類的方法,
這個(gè)也可以判斷是否覆蓋的父類方法,在方法前面加上此語句,如果提示的錯(cuò)誤,那么你不是覆蓋的父類的方法,要是提示的沒有錯(cuò)誤,那么就是覆蓋的父類的方法。
@SuppressWarnings("deprecation"):取消編譯器的警告(例如你使用的方法過時(shí)了)
@Deprecated:在方法的最上邊也上此語句,表示此方法過時(shí),了,或者使用在類上面
import java.util.ArrayList;
import java.util.List;
public class annotationDemo {
/*
* 對于集合,如果沒有指定存儲的類型,那么就會有安全警告,
* 如果不想提示安全警告的話,那么就所在類或者方法上添加@SuppressWarnings(參數(shù))
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List list=new ArrayList();
}
}
2.自定義注解
1.格式
權(quán)限 @interface 注解名稱 { }
步驟:
定義注解類--->定義應(yīng)用注解類的類--->對應(yīng)用注解類的類進(jìn)行反射的類(這個(gè)類可以另外定義,也可以是在應(yīng)用注解類中進(jìn)行測試)
import java.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
//定義此注解保留在字節(jié)碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
@MyAnnotation
// 應(yīng)用定義的注解類
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(參數(shù)類型) 參數(shù)類型是RetentionPolicy
RetentionPolicy.CLASS:類文件上,運(yùn)行時(shí)虛擬機(jī)不保留注解
RetentionPolicy.RUNTIME:類文件上,運(yùn)行時(shí)虛擬就保留注解
RetentionPolicy.SOURCE:源文件上,丟棄注解
SuppressWarnings和Override是RetentionPolicy.SOURCE,
Deprecated是在RetentionPolicy.RUNTIME,要向運(yùn)行時(shí)調(diào)用定義的一樣,那么必須是RetentionPolicy.RUNTIME,
默認(rèn)的都是RetentionPolicy.CLASS:
3.指定目標(biāo)
格式:例如:方法上@Target(ElementType.METHOD)
定義的注解可以注解什么成員。如果不聲明此注解,那么就是可以放到任何程序的元素上。
可以是包,接口,參數(shù),方法,局部變量,字段…等。
//定義此注解保留在字節(jié)碼中
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})//可以定義在方法上和類上接口,表示類型
public @interface MyAnnotation {
}
@MyAnnotation
// 應(yīng)用定義的注解類
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個(gè)基本數(shù)據(jù)類型,String,枚舉,注解,Class,數(shù)組類型,
2.注意點(diǎn)
當(dāng)注 解中只有一個(gè)屬性或者是只有一個(gè)屬性需要賦值的話,那么在調(diào)用的時(shí)候,就可以直接寫入,不需要指定屬性名,
當(dāng)注解的屬性是數(shù)組類型并且賦值的時(shí)候只賦值一個(gè)值,那么就可以省略{}.
3.示例
3.1.屬性類型(是String)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//定義此注解保留在字節(jié)碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//設(shè)置默認(rèn)值是"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());
}
}
}
結(jié)果:
value=java
Color=red
從調(diào)用的程序中,也可以看出,只有一個(gè)屬性可以需要賦值的話,可以省略屬性名。否則@注解類(屬性名=值)
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.*;
//定義此注解保留在字節(jié)碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//設(shè)置默認(rèn)值是"red"
Week week() default Week.MON;//枚舉類型
int [] array() default {1,2,3};//數(shù)組類型
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)//數(shù)組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());
}
}
}
結(jié)果:
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");
}
}
結(jié)果:java
- Java注解之Retention、Documented、Inherited介紹
- 5分鐘搞懂java注解@Annotation的具體使用
- Java注解Annotation與自定義注解詳解
- Java注解機(jī)制之Spring自動裝配實(shí)現(xiàn)原理詳解
- Java注解@Transactional事務(wù)類內(nèi)調(diào)用不生效問題及解決辦法
- 基于Java注解(Annotation)的自定義注解入門介紹
- 詳解Java注解教程及自定義注解
- 創(chuàng)建自定義的Java注解類的方法
- 深入理解Java注解類型(@Annotation)
- 輕松掌握J(rèn)ava注解,讓編程更智能、更優(yōu)雅
相關(guān)文章
Java?Bluetooth?藍(lán)牙通訊?BlueCove?掃描附近的藍(lán)牙設(shè)備(測試代碼)
BlueCove是一個(gè)開源的藍(lán)牙協(xié)議棧實(shí)現(xiàn),旨在為Java開發(fā)者提供一個(gè)全面的、易于使用的API,從而在應(yīng)用程序中實(shí)現(xiàn)藍(lán)牙功能,該項(xiàng)目支持多種操作系統(tǒng),這篇文章主要介紹了Java?Bluetooth?藍(lán)牙通訊?BlueCove?掃描附近的藍(lán)牙設(shè)備,需要的朋友可以參考下2025-01-01
Java利用Picocli開發(fā)一個(gè)簡化命令行工具
Picocli 是一個(gè)強(qiáng)大、易用且功能豐富的 Java 庫,用于開發(fā)命令行工具,本文我們就來為大家介紹一下Java如何利用Picocli進(jìn)行命令行簡化功能的吧2025-03-03
Java Map遍歷2種實(shí)現(xiàn)方法代碼實(shí)例
這篇文章主要介紹了Java Map遍歷2種實(shí)現(xiàn)方法代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
java讀取excel圖片導(dǎo)入代碼示例(親測有效)
在日常工作中,我們經(jīng)常要將一些照片插入到Excel表格中,這篇文章主要給大家介紹了關(guān)于java讀取excel圖片導(dǎo)入的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
Java實(shí)現(xiàn)整數(shù)分解質(zhì)因數(shù)的方法示例
這篇文章主要介紹了Java實(shí)現(xiàn)整數(shù)分解質(zhì)因數(shù)的方法,結(jié)合實(shí)力形式分析了質(zhì)因數(shù)分解的原理與實(shí)現(xiàn)方法,涉及java數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
java 服務(wù)器接口快速開發(fā)之servlet詳細(xì)教程
Servlet(Server Applet)是Java Servlet的簡稱,稱為小服務(wù)程序或服務(wù)連接器,用Java編寫的服務(wù)器端程序,具有獨(dú)立于平臺和協(xié)議的特性,主要功能在于交互式地瀏覽和生成數(shù)據(jù),生成動態(tài)Web內(nèi)容2021-06-06
java實(shí)戰(zhàn)小技巧之優(yōu)雅的實(shí)現(xiàn)字符串拼接
字符串拼接是我們在Java代碼中比較經(jīng)常要做的事情,就是把多個(gè)字符串拼接到一起,這篇文章主要給大家介紹了關(guān)于java實(shí)戰(zhàn)小技巧之優(yōu)雅的實(shí)現(xiàn)字符串拼接的相關(guān)資料,需要的朋友可以參考下2021-08-08

