欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java教程之java注解annotation使用方法

 更新時(shí)間:2014年01月28日 14:59:39   作者:  
這篇文章主要介紹了java注解annotation使用方法,注解可以定義到方法上,類上,一個(gè)注解相當(dāng)與一個(gè)類,就相當(dāng)于實(shí)例了一個(gè)對(duì)象,加上了注解,就相當(dāng)于加了一個(gè)標(biāo)志

1.概述

注解可以定義到方法上,類上,一個(gè)注解相當(dāng)與一個(gè)類,就相當(dāng)于實(shí)例了一個(gè)對(duì)象,加上了注解,就相當(dāng)于加了一個(gè)標(biāo)志。

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

復(fù)制代碼 代碼如下:

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

2.自定義注解

1.格式
權(quán)限 @interface 注解名稱 { }
步驟:
定義注解類--->定義應(yīng)用注解類的類--->對(duì)應(yīng)用注解類的類進(jìn)行反射的類(這個(gè)類可以另外定義,也可以是在應(yīng)用注解類中進(jìn)行測試)

復(fù)制代碼 代碼如下:

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ù),方法,局部變量,字段…等。

復(fù)制代碼 代碼如下:

//定義此注解保留在字節(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)

復(fù)制代碼 代碼如下:

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.綜合類型

復(fù)制代碼 代碼如下:

/*枚舉類*/
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é)果:
 

復(fù)制代碼 代碼如下:

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

4.Method上的注解

復(fù)制代碼 代碼如下:

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

相關(guān)文章

  • Spring的InitializingBean接口解析

    Spring的InitializingBean接口解析

    這篇文章主要介紹了Spring的InitializingBean接口解析,InitializingBean是spring為bean的初始化提供了一種新的方式,里面只有一個(gè)方法afterPropertiesSet,作用就是實(shí)現(xiàn)這個(gè)接口或者實(shí)現(xiàn)了繼承InitializingBean的方法的bean都要執(zhí)行這個(gè)方法,需要的朋友可以參考下
    2024-02-02
  • 一文了解Java?線程池的正確使用姿勢

    一文了解Java?線程池的正確使用姿勢

    線程池在平時(shí)的工作中出場率非常高,基本大家多多少少都要了解過,可能不是很全面,本文和大家基于jdk8學(xué)習(xí)下線程池的全面使用,以及分享下使用過程中遇到的一些坑,希望對(duì)大家有所幫助
    2022-10-10
  • Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格

    Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格

    這篇文章主要為大家詳細(xì)介紹了Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Java8中Lambda表達(dá)式使用和Stream API詳解

    Java8中Lambda表達(dá)式使用和Stream API詳解

    這篇文章主要給大家介紹了關(guān)于Java8中Lambda表達(dá)式使用和Stream API的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java8具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 深入了解Spring Boot2.3.0及以上版本的Liveness和Readiness功能

    深入了解Spring Boot2.3.0及以上版本的Liveness和Readiness功能

    這篇文章主要介紹了Spring Boot2.3.0及以上版本的Liveness和Readiness功能示例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • EL調(diào)用Java方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    EL調(diào)用Java方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    簡單來說,我們在一個(gè)類中的某個(gè)方法,可以使用EL進(jìn)行調(diào)用,這個(gè)能被EL表達(dá)式調(diào)用的方法稱之為EL函數(shù),但是這種方式必須滿足兩點(diǎn)要求,具體哪兩點(diǎn),大家可以參考下本文
    2017-07-07
  • Spring中使用AOP進(jìn)行事務(wù)管理實(shí)例

    Spring中使用AOP進(jìn)行事務(wù)管理實(shí)例

    這篇文章主要介紹了Spring中使用AOP進(jìn)行事務(wù)管理實(shí)例,當(dāng)在Spring項(xiàng)目中涉及數(shù)據(jù)庫操作時(shí),事務(wù)管理是非常重要的,它可以確保數(shù)據(jù)庫操作的一致性和完整性,Spring提供了強(qiáng)大的事務(wù)管理功能,可以通過聲明式或編程式兩種方式進(jìn)行配置,需要的朋友可以參考下
    2023-09-09
  • SpringCloud基于Feign的可編程式接口調(diào)用實(shí)現(xiàn)

    SpringCloud基于Feign的可編程式接口調(diào)用實(shí)現(xiàn)

    本文主要介紹了SpringCloud基于Feign的可編程式接口調(diào)用實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Spring Cloud負(fù)載均衡及遠(yuǎn)程調(diào)用實(shí)現(xiàn)詳解

    Spring Cloud負(fù)載均衡及遠(yuǎn)程調(diào)用實(shí)現(xiàn)詳解

    這篇文章主要介紹了Spring Cloud負(fù)載均衡及遠(yuǎn)程調(diào)用實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Mac系統(tǒng)搭建JDK及JMETER過程解析

    Mac系統(tǒng)搭建JDK及JMETER過程解析

    這篇文章主要介紹了Mac系統(tǒng)搭建JDK及JMETER過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08

最新評(píng)論