Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ inherited meta-annotations in java projects

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to use @Inherited meta annotation in java project, the content is detailed and easy to understand, the operation is simple and fast, and it has certain reference value. I believe everyone will gain something after reading this article on how to use @Inherited meta annotation in java project. Let's take a look at it together.

1. First look at the source documentation

@ Recorded

@Retention. RUNTIME)

@Target(ElementType。comment type)

public@interfaceInherited{

The above code can be translated by Google. general meaning is

Indicates automatic inheritance of annotation types. If there is an inherited meta label in the label type declaration, the user queries the label type on the class declaration, but the class declaration does not have a label of this type, then the superclass of the class automatically queries the label type. This process is repeated until this type of comment is found or the top of the class hierarchy is reached. If no superclass has this type of comment, the query will indicate that the class has no such comment.

Note that if you use comment types to annotate anything other than classes, this meta-comment type has no effect. Note also that this meta comment only results in comments inherited from superclasses; invalid comments for implemented interfaces.

As you can see from the description above, children of the parent class of labels that use this label can inherit labels from the parent class.

2. code testing

2.1 has @Inherited annotation

@Target(ElementType。Type)

@Retention. RUNTIME)

@ Inheritance

public @ interfaceInheritedTest {

string value();

}@InheritedTest ('own inheritance')

publicclassPerson{

publicvoidmethod(){

}

publicvoidmethod2(){

}

} publicclassstudentextends person {

} Testing:

publicclassTestInherited{

public static void main(String[]args){

CLaSS studentCLaSS=student . class;

if(student class . is notationpresent(inherited test . class)){

system . out . println(StudentClass . GetAnnotation(InheritedTest . Class))。value());

}

}

} Output:

2.2 No @Inherited comments

@Target(ElementType。Type)

@Retention. RUNTIME)

public @ interfacesinotinherited {

string value();

} @ IsNotInherited ('does not own inheritance')

publicclassPerson{

publicvoidmethod(){

}

publicvoidmethod2(){

}

} publicclassstudentextends person {

} Testing:

publicclassTestInherited{

public static void main(String[]args){

CLaSS studentCLaSS=student . class;

if(StudentClass . is notationPresent(iSnotinerIded . Class)){

system . out . println(StudentClass . GetAnnotation(iSNOTiHerided . class))。value());

}

}

}

No tolerance is output, so it can be seen that subclasses of classes without the @Inherited annotation do not inherit it.

The content of this article on "How to use @Inherited meta annotation in java project" is introduced here, thank you for reading! I believe everyone has a certain understanding of "how to use @Inherited meta annotation in java project" knowledge. If you still want to learn more knowledge, please pay attention to industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report