栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java原生注解@Inherited的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java原生注解@Inherited的使用

java原生注解@Inherited

@Inherited作为java的原生注解之一,表示其标记的注解允许被继承,接口实现除外。

1、自定义两个注解SleepTime和ActiveTime,其中一个有@Inherited标记,另一个没有@Inherited标记

package com.chengya.demo.test;

import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;

@Target({ElementType.TYPE,ElementType.METHOD})
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface SleepTime {
    int max() default 60;

    TimeUnit timeUnit() default TimeUnit.SECONDS;
}

package com.chengya.demo.test;

import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ActiveTime {
    int min() default 10;

    TimeUnit timeUnit() default TimeUnit.SECONDS;
}

2、自定义接口Animal,并用Cat实现它

@SleepTime(max = 120)
@ActiveTime(min = 5)
public interface Animal {
    
}
public class Cat implements Animal {
    
}

3、自定义父类Dog,并用WhiteDog继承它

@SleepTime(max = 360)
@ActiveTime(min = 60)
public class Dog {
    
}
public class WhiteDog extends Dog {
    
}

4、测试

package com.chengya.demo.test;

import java.lang.annotation.Annotation;

public class ClientTest {
    public static void main(String[] args) throws NoSuchMethodException {
        System.out.println("======Animal Class Annotation======");
        Class animalClass = Animal.class;
        Annotation[] sleepAnnotations = animalClass.getAnnotations();
        for (Annotation annotation : sleepAnnotations) {
            System.out.println(annotation.toString());
        }

        System.out.println("======Cat Class Annotation======");
        Class catClass = Cat.class;
        Annotation[] catAnnotations = catClass.getAnnotations();
        for (Annotation annotation : catAnnotations) {
            System.out.println(annotation.toString());
        }

        System.out.println("======Dog Class Annotation======");
        Class dogClass = Dog.class;
        Annotation[] dogAnnotations = dogClass.getAnnotations();
        for (Annotation annotation : dogAnnotations) {
            System.out.println(annotation.toString());
        }

        System.out.println("======White Dog Class Annotation======");
        Class whiteDogClass = WhiteDog.class;
        Annotation[] whiteDogAnnotations = whiteDogClass.getAnnotations();
        for (Annotation annotation : whiteDogAnnotations) {
            System.out.println(annotation.toString());
        }
    }
}

输出结果:

----------Animal Class Annotation----------
@com.chengya.demo.test.SleepTime(max=120, timeUnit=SECONDS)
@com.chengya.demo.test.ActiveTime(min=5, timeUnit=SECONDS)
----------Cat Class Annotation----------
----------Dog Class Annotation----------
@com.chengya.demo.test.SleepTime(max=360, timeUnit=SECONDS)
@com.chengya.demo.test.ActiveTime(min=60, timeUnit=SECONDS)
----------White Dog Class Annotation----------
@com.chengya.demo.test.SleepTime(max=360, timeUnit=SECONDS)

由此可见,如果父类使用的注解带有@Inherited标注,子类是可以获取到此注解的;如果父类使用的注解不带@Inherited标注,子类则无法获取到此注解。

至于接口,无论接口使用的注解是否带有@Inherited标注,实现类都是无法获取到接口的注解的。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/750785.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号