日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java8新特性探究(5):重復(fù)注解

 什么是重復(fù)注解

允許在同一申明類型(類,屬性,或方法)的多次使用同一個(gè)注解

一個(gè)簡單的例子

java 8之前也有重復(fù)使用注解的解決方案,但可讀性不是很好,比如下面的代碼:

 
 
  1. public @interface Authority {  
  2.      String role();  
  3. }  
  4.  
  5. public @interface Authorities {  
  6.     Authority[] value();  
  7. }  
  8.  
  9. public class RepeatAnnotationUseOldVersion {  
  10.       
  11.     @Authorities({@Authority(role="Admin"),@Authority(role="Manager")})  
  12.     public void doSomeThing(){  
  13.     }  
  14. }  

由另一個(gè)注解來存儲(chǔ)重復(fù)注解,在使用時(shí)候,用存儲(chǔ)注解Authorities來擴(kuò)展重復(fù)注解,我們?cè)賮砜纯磈ava 8里面的做法:

 
 
  1. @Repeatable(Authorities.class)  
  2. public @interface Authority {  
  3.      String role();  
  4. }  
  5.  
  6. public @interface Authorities {  
  7.     Authority[] value();  
  8. }  
  9.  
  10. public class RepeatAnnotationUseNewVersion {  
  11.     @Authority(role="Admin")  
  12.     @Authority(role="Manager")  
  13.     public void doSomeThing(){ }  
  14. }  

不同的地方是,創(chuàng)建重復(fù)注解Authority時(shí),加上@Repeatable,指向存儲(chǔ)注解Authorities,在使用時(shí)候,直接可以重復(fù)使用Authority注解。從上面例子看出,java 8里面做法更適合常規(guī)的思維,可讀性強(qiáng)一點(diǎn)

總結(jié)

JEP120沒有太多內(nèi)容,是一個(gè)小特性,僅僅是為了提高代碼可讀性。這次java 8對(duì)注解做了2個(gè)方面的改進(jìn)(JEP 104,JEP120),相信注解會(huì)比以前使用得更加頻繁了。

原文鏈接:http://my.oschina.net/benhaile/blog/180932


分享標(biāo)題:Java8新特性探究(5):重復(fù)注解
當(dāng)前URL:http://www.5511xx.com/article/coiijsi.html