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 Java custom annotations

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use Java custom annotations", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Java custom annotations" this article.

The details are as follows:

A custom annotation syntax

Name of [public] @ interface Annotation {[data type variable name ();]}

To customize annotations, you need to use @ interface to define them, and you can define various variables when defining annotations, but the variables must be followed by parentheses ().

Tip: using @ interface is the opposite of inheriting the Annotation interface. If you use @ interface to declare Annotation in your program, this Annotation actually inherits the Annotation interface.

Second, custom comments that do nothing.

1 code

/ / MyAnnoation annotations are defined, where no variables are defined. Here is only a demo framework @ interface MyAnnoation {/ / do nothing} class Test {/ / demonstrating the use of custom annotations @ MyAnnoation public static void main (String [] args) {System.out.println ("hello MyAnnoation");}}

2 running result

Hello MyAnnoation

Three custom annotations that define variables

1 code

/ / defines the AnnoationVar annotation, which declares two variables, key and value. @ interface AnnoationVar {public String key (); public String value ();} class TestAnnoationVar {/ / uses the form of "variable name = value" to assign @ AnnoationVar (key= "var1", value = "test") public static void main (String [] args) {System.out.println ("hello AnnoationVar");}}

2 running result

Hello AnnoationVar

3 pay attention to

If you declare a variable in a custom annotation, but do not set a default value, you must assign a value to the variable when using the annotation.

Four default values are set in custom variables

1 code

/ / defines annotation AnnoationDefault, declares two variables key and value, and sets the default values @ interface AnnoationDefault {public String key () default "var1"; public String value () default "value1";} class TestAnnoationDefault {/ / uses annotations and does not assign values to variables. The compiler automatically uses the default values to assign @ AnnoationDefault () public static void main (String [] args) {System.out.println ("hello TestAnnoationDefault");}}

2 running result

Hello TestAnnoationDefault

Application of five enumerations in custom annotations

1 code

@ interface AnnoationEnum {public Color color ();} enum Color {RED,GREEN,BLUE;} class TestAnnoationEnum {@ AnnoationEnum (color = Color.BLUE) public static void main (String [] args) {System.out.println ("hello TestAnnoationEnum");}}

2 running result

Hello TestAnnoationEnum

The above is all the content of this article "how to use Java Custom Notes". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the 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

Development

Wechat

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

12
Report