@Inherited注解标记其他的注解用于指明标记的注解是可以被自动继承的。
注意:此注解只对注解标记的超类有效,对接口是无效的。
示例:
先声明两个用@Inherited标记的注解,@Name和@Abbreviation,一个没有用@Inherited标记的注解@UpperCaseName
package org.springmorning.demo.javabase.annotation.meta;
import java.lang.annotation.*;
/**
* @author 春晨
* @date 2019/1/14 15:49
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Name {
//名称
String value();
}
package org.springmorning.demo.javabase.annotation.meta;
import java.lang.annotation.*;
/**
* @author 春晨
* @date 2019/1/14 14:59
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Abbreviation {
//简称
String value();
}
package org.springmorning.demo.javabase.annotation.meta;
import java.lang.annotation.*;
/**
* @author 春晨
* @date 2019/1/14 11:19
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface UpperCaseName {
//大写英文名称
String value();
}
接下来,我们声明一些接口和类用于举例,这些类的继承关系如下:
package org.springmorning.demo.javabase.annotation.meta;
/**
* @author 春晨
* @date 2019/1/14 15:57
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
@UpperCaseName("ORGANISM")
@Abbreviation("Ogm")
@Name("Organism")
public interface Organism {
}
package org.springmorning.demo.javabase.annotation.meta;
/**
* @author 春晨
* @date 2019/1/14 15:24
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
public interface Plant extends Organism{
}
package org.springmorning.demo.javabase.annotation.meta;
/**
* @author 春晨
* @date 2019/1/14 15:30
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
@UpperCaseName("FLOWER")
@Abbreviation("Flr")
@Name("Flower")
public abstract class Flower implements Plant {
}
package org.springmorning.demo.javabase.annotation.meta;
/**
* @author 春晨
* @date 2019/1/14 16:00
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
public class Rose extends Flower {
}
package org.springmorning.demo.javabase.annotation.meta;
/**
* @author 春晨
* @date 2019/1/14 11:25
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
@UpperCaseName("ANIMAL")
@Abbreviation("Ani")
@Name("Animal")
public interface Animal extends Organism{
}
package org.springmorning.demo.javabase.annotation.meta;
/**
* @author 春晨
* @date 2019/1/14 11:30
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
public abstract class Mammal implements Animal {
}
package org.springmorning.demo.javabase.annotation.meta;
/**
* @author 春晨
* @date 2019/1/14 11:34
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
@UpperCaseName("MONKEY")
@Abbreviation("Mky")
@Name("Monkey")
public class Monkey extends Mammal{
}
package org.springmorning.demo.javabase.annotation.meta;
/**
* @author 春晨
* @date 2019/1/14 16:26
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
@Name("Roxellanae")
public class Roxellanae extends Monkey {
}
以下我们创建一个测试类:
package org.springmorning.demo.javabase.annotation.meta;
import java.lang.annotation.Annotation;
/**
* @author 春晨
* @date 2019/1/14 11:39
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10268727.html
*/
public class InheritedAnnotation {
public static void main(String\[\] args){
Annotation\[\] annotations = Plant.class.getAnnotations();
System.out.print("Plant接口继承Organism接口,在Organism接口上标记的注解,Plant获取不到:");
for (Annotation annotation : annotations){
System.out.print(annotation.toString()+" ");
}
annotations = Mammal.class.getAnnotations();
System.out.print("\\nMamanl抽象类实现Animal接口,在Animal接口上标记的注解,Mammal获取不到:");
for (Annotation annotation : annotations){
System.out.print(annotation.toString()+" ");
}
annotations = Rose.class.getAnnotations();
System.out.print("\\nRose类继承Flower抽象类,在Flower抽象类上标记的注解,如果注解是被@Inherited标记的,都可以获取到:");
for (Annotation annotation : annotations){
System.out.print(annotation.toString()+" ");
}
annotations = Roxellanae.class.getAnnotations();
System.out.print("\\nRoxellanae类继承Monkey类,在Monkey抽象类上标记的注解,如果注解是被@Inherited标记的,都可以获取到;如果被@Inherited标记的注解父类和子类重复标记,则返回子类的注解:");
for (Annotation annotation : annotations){
System.out.print(annotation.toString()+" ");
}
}
}
输出结果:
Plant接口继承Organism接口,在Organism接口上标记的注解,Plant获取不到:
Mamanl抽象类实现Animal接口,在Animal接口上标记的注解,Mammal获取不到:
Rose类继承Flower抽象类,在Flower抽象类上标记的注解,如果注解是被@Inherited标记的,都可以获取到:@org.springmorning.demo.javabase.annotation.meta.Abbreviation(value=Flr) @org.springmorning.demo.javabase.annotation.meta.Name(value=Flower)
Roxellanae类继承Monkey类,在Monkey抽象类上标记的注解,如果注解是被@Inherited标记的,都可以获取到;如果被@Inherited标记的注解父类和子类重复标记,则返回子类的注解:@org.springmorning.demo.javabase.annotation.meta.Abbreviation(value=Mky) @org.springmorning.demo.javabase.annotation.meta.Name(value=Roxellanae)
通过运行结果我们可以得出如下三点结论:
下节将给大家讲解元注解@Repeatable的使用
手机扫一扫
移动阅读更方便
你可能感兴趣的文章