我是靠谱客的博主 冷傲日记本,这篇文章主要介绍Java8新特性——重复注解使用,现在分享给大家,希望可以做个参考。

1、定义重复的注解容器注解

2、定义一个可以重复的注解

import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@MyTest("ca")
@MyTest("cb")
public class Demo4 {
@MyTest("ma")
@MyTest("mb")
public void show() {
}
public static void main(String[] args) {
MyTest[] annotationsByType = Demo4.class.getAnnotationsByType(MyTest.class);
for (MyTest myTest : annotationsByType) {
System.out.println(myTest);
}
System.out.println("--------------------");
try {
MyTest[] shows = Demo4.class.getMethod("show").getAnnotationsByType(MyTest.class);
for (MyTest show : shows) {
System.out.println(show);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
/**
* 1、定义重复的注解容器注解
*/
@Retention(RetentionPolicy.RUNTIME)
@interface MyTests {
MyTest[] value();
}
/**
* 2、定义一个可以重复的注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(MyTests.class)
@interface MyTest {
String value();
}

 

最后

以上就是冷傲日记本最近收集整理的关于Java8新特性——重复注解使用的全部内容,更多相关Java8新特性——重复注解使用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(113)

评论列表共有 0 条评论

立即
投稿
返回
顶部