?? According to your own study plan, it is time to write something today. In the previous article, we mentioned JUnit's study, which involved some built-in annotation, such as @test, @Ignore, etc. Now I'm talking about how to customize my own annotation with a personal understanding. Annotation can be used to associate any information with a program element (class, method, member variable, etc.), but annotaion does not affect the execution of the program code, and the code is executed consistently regardless of the addition or deletion of annotation. In addition, while some annotation are accessed at run time through the Java Reflection API approach, the Java language interpreter ignores these annotation when it works. It is because the Java virtual machine ignores annotation, which causes the annotation type to "not work" in the code, and is easy to use only if the information in the annotation type is accessed and processed by a matching tool. First, define annotation syntax. @interface is a keyword that must be defined as @interface when customizing its own annotations, and not as a class or interface keyword, such as "public @interface myannotation{} ", here are a few examples of definitions:? 1. No method/attribute annotation?? 2, with a method GetValue () of the Annotation3,? There is a special method of value () of the annotation (which is mentioned below) 4, which has a method and a property of the Annotation5, annotation with multiple methods can be seen in the example above, In fact, a annotation and interface, class is different from the keyword @interface, and it can also set the default return value of the method, such as "? public int getsinglenumber () default 0;???? , set the default to return 0. There is a significant difference in stating that each annotation has several tags @target, @Retention. The target of annotation is a program element that is annotated. Target illustrates the range of objects that annotation modifies: annotation can be used for packages, types (classes, interfaces, enumerations, annotation types), type members (methods, construction methods, member variables, enumeration values), method parameters, and local variable(such as a loop variable, catch parameter). Target can be more clearly decorated by using target in the declaration of the annotation type. Annotation's retention defines how long the annotation is retained: Some annotation appear only in the source code and are discarded by the compiler, while others are compiled in the class file The annotation that are compiled in the class file may be ignored by the virtual machine, while others will be read when class is loaded (note that it does not affect class execution because annotation is separated from the class in use). Using this meta-annotation can limit the "life cycle" of annotation. Retentionpolicy is an enum type with a total of three values, namely Source,class and RUNTIME. Source represents the annotation type of information will only be retained in the program source code, if the source code is compiled, the annotation data will disappear, and will not be kept in the compiled. class file. Class means that this annotation type of information is kept in the program source code, but also in the compiled. class file, which does not load this information into the virtual machine (JVM) when executed. . Note that when you do not set a retention value for a annotation type, the system default value is class. The third, is runtime, said in the source code, The compiled. class file retains information that is loaded into the JVM when it is executed. Here's how to use your own defined annotation:? From the test code above you can see that using a annotation syntax is by"@+annotationtype name+(..comma-separatedName-valuethe...) "composition. Where members can follow any order. Ifannotationtype defines the default value for a member, the member can be omitted. The member value must be a compile-time, inlineannotationor an array. The above mentioned a particularity, we can compare the above testMethod2 and testMethod3 on the annotation, will find the value of @onemethodspecialinannotation"hello,world! "There is no match for name, so this valuewho did it pass to? There is actually a convention here. If there is no value for the Write property name, and this comment hasvalueattribute, the value is assigned to thevalueproperty, which is equivalent to (value= "hello,world! ")。 The Main method is a test of the above, and its output is:?o (∩_∩) o
It is said that men look at the United States map can prolong life, women to look at the United States map can beauty ...
So, where to look at the beautiful map? Honey, don't worry! In here---
Welcome attention! Light and Shadow Portrait of the public number!
Light and shadow portraits---and beauty.
(Portrait of a photographic art figure)
Copyright 2015 Lighting Technology Co., ltd All rights reserved
Reply 1: Home
Reply 2: Shadow Gallery
Reply 3: Community
Reply 4: Download app
Reply 5: Featured Articles List
Lightshadow Shadow portrait Abstract art macro Macro Animals Animal Nature natural black and white black-people portrait Celebrities Festival performing Arts line For Art City Urban Architecture building Sport Sport Commercial Commercial photography life Concert Street Family family transportation Fashion Fashion Travel Tour Film movie underwater underwater Fine Art Urban country exploration food gourmet wedding wedding journalism Uncategorized Miscellaneous Landscapes Landscape
Learning Notes Java Annotation Learning summary [shadow Portrait of Donghai Chen Guangjin Blog]