Inherited什么意思详细解释下
@Inherited是Java中的一个注解,用于标注某个父类的注解能否被其子类继承。具体来说,如果一个注解被@Inherited标注过,则其子类会继承该注解,并且可以在子类中使用该注解。如果一个注解没有被@Inherited标注过,则其子类不会继承该注解。
举个例子,假设有一个注解@MyAnnotation,它被@Inherited标注过。现在有一个父类Parent,上面标注了@MyAnnotation,那么子类Child如果没有标注@MyAnnotation,则会继承Parent上的@MyAnnotation;如果Child上标注了@MyAnnotation,则会使用Child上的@MyAnnotation。如果@MyAnnotation没有被@Inherited标注过,则Child不会继承Parent上的@MyAnnotation。
需要注意的是,@Inherited只对类级别的注解有效,对方法级别的注解无效。同时,注解本身必须被定义为@Inherited才能被其子类继承。
原文地址: http://www.cveoy.top/t/topic/biys 著作权归作者所有。请勿转载和采集!