关于java中元注解Inherited 的使用说明
首先解释下元注解,就是用来中声明注解类型时需要使用到的注解。
Inherited作用是,使用此注解声明出来的自定义注解,在使用此自定义注解时,如果注解在类上面时,子类会自动继承此注解,否则的话,子类不会继承此注解。这里一定要记住,使用Inherited声明出来的注解,只有在类上使用时才会有效,对方法,属性等其他无效。
下面看下代码就明了了。
- /**
- * 声明的此注解使用了Inherited元注解,表示此注解用在类上时,会被子类所继承
- * @author crazy
- */
- @Retention(RetentionPolicy.RUNTIME)
- @Inherited
- public @interface InheritedTest {
- String value();
- }
- /**
- * 声明的此注解没有使用Inherited元注解,表示此注解用在类上时,不会被子类所继承
- * @author crazy
- */
- @Retention(RetentionPolicy.RUNTIME)
- public @interface InheritedTest2 {
- String value();
- }
- /**
- * 父类
- * @author crazy
- */
- @InheritedTest("使用Inherited的注解 class")
- @InheritedTest2("未使用Inherited的注解 class")
- public class Parent {
- @InheritedTest("使用Inherited的注解 method")
- @InheritedTest2("未使用Inherited的注解 method")
- public void method(){
- }
- @InheritedTest("使用Inherited的注解 method2")
- @InheritedTest2("未使用Inherited的注解 method2")
- public void method2(){
- }
- @InheritedTest("使用Inherited的注解 field")
- @InheritedTest2("未使用Inherited的注解 field")
- public String a;
- }
- /**
- * 子类 只继承了一个method方法
- * @author crazy
- */
- public class Child extends Parent {
- @Override
- public void method() {
- }
- }
- /**
- * 通过反射进行测试
- * @author crazy
- */
- public class test {
- public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {
- Class<Child> clazz = Child.class;
- //对类进行测试
- System.out.println("对类进行测试");
- if(clazz.isAnnotationPresent(InheritedTest.class)){
- System.out.println(clazz.getAnnotation(InheritedTest.class).value());
- }
- if(clazz.isAnnotationPresent(InheritedTest2.class)){
- System.out.println(clazz.getAnnotation(InheritedTest2.class).value());
- }
- System.out.println();
- //对方法 进行测试
- System.out.println("对方法进行测试");
- Method method = clazz.getMethod("method", null);
- if(method.isAnnotationPresent(InheritedTest.class)){
- System.out.println(method.getAnnotation(InheritedTest.class).value());
- }
- if(method.isAnnotationPresent(InheritedTest2.class)){
- System.out.println(method.getAnnotation(InheritedTest2.class).value());
- }
- System.out.println();
- //对方法2 进行测试
- System.out.println("对方法2进行测试");
- Method method2 = clazz.getMethod("method2", null);
- if(method2.isAnnotationPresent(InheritedTest.class)){
- System.out.println(method2.getAnnotation(InheritedTest.class).value());
- }
- if(method2.isAnnotationPresent(InheritedTest2.class)){
- System.out.println(method2.getAnnotation(InheritedTest2.class).value());
- }
- System.out.println();
- //对属性测试
- System.out.println("对属性进行测试");
- Field field = clazz.getField("a");
- if(field.isAnnotationPresent(InheritedTest.class)){
- System.out.println(field.getAnnotation(InheritedTest.class).value());
- }
- if(field.isAnnotationPresent(InheritedTest2.class)){
- System.out.println(field.getAnnotation(InheritedTest2.class).value());
- }
- }
- }
下面是输出结果
- 对类进行测试
- 使用Inherited的注解 class
- 对方法进行测试
- 对方法2进行测试
- 使用Inherited的注解 method2
- 未使用Inherited的注解 method2
- 对属性进行测试
- 使用Inherited的注解 field
- 未使用Inherited的注解 field
由上可以看出,通过Inherited元注解声明的自定义注解,在类上使用时,可以被子类继承,对第一个方法进行测试时,由于子类继承了父类方法,且两个都没有输出,证明Inherited对方法无效,由方法2可以看出,因为子类没有重写父类方法,所以是直接使用的父类方法,所以两个都会输出,同理属性也是,都会输出。
所以证明最后结论:通过对注解上使用元注解Inherited声明出的注解,在使用时用在类上,可以被子类所继承,对属性或方法无效。
本文详细解析了Java中Inherited元注解的作用及其应用场景,通过实例代码展示了如何使用Inherited实现注解的继承特性,并强调了其仅适用于类级别的限制。

620

被折叠的 条评论
为什么被折叠?



