Dark Horse programmer --- & lt; Basic enhancement --- 1.5 new features (in) (Annotation ))&

Source: Internet
Author: User

1. note 1. the overview annotation can be defined on the method. On the class, an annotation is equivalent to a class, which is equivalent to an object of the instance. With the annotation, it is equivalent to adding a flag. Commonly used annotation: @ Override: indicates the method for re-parent class. This can also determine whether to overwrite the parent class method. Add this statement before the method. If an error is prompted, then you are not the method of overwriting the parent class. If no error is prompted, it is the method of overwriting the parent class. @ SuppressWarnings ("deprecation"): cancels the compiler warning (for example, if your method is outdated) @ Deprecated: the statement is used at the top of the method, indicating that the method is outdated, or use the above example in the class: cancel the warning [java] import java. util. arrayList; import java. util. list; public class annotationDemo {/** for a set, if the storage type is not specified, a security warning will be triggered. * if you do not want to receive a security warning, add @ SuppressWarnings (parameter) */@ SuppressWarnings ("unchecked") public static void main (String [] args) to the class or method) {List list = new ArrayList () ;}} 2. custom annotation 1. format right @ Interface annotation name {} Step: Define annotation class ---> define the class of the application annotation class ---> reflect the class of the application annotation class (this class can be defined separately, it can also be tested in the application annotation class.) [java] import java. lang. annotation. retention; importjava. lang. annotation. retentionPolicy; // defines that this annotation is kept in the bytecode @ Retention (RetentionPolicy. RUNTIME) public @ interface MyAnnotation {}@ MyAnnotation // annotation class defined by the application public class ApplyMyAnnotation {public static void main (String [] args) {if (ApplyMyAnnotation. class. isAnno TationPresent (MyAnnotation. class) {// determine whether the specified annotation class MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation exists on this class. class. getAnnotation (MyAnnotation. class); System. out. println (annotation) ;}} result: @ www. fuxi. jiaqiang. myAnnotation () 2. statement cycle format: for example, @ Retention (RetentionPolicy. CLASS) defines the cycle in the Custom annotation CLASS. @ Retention (parameter type) the parameter type is RetentionPolicy. CLASS: The RetentionPolicy annotation is not retained by the virtual machine during running on the CLASS file. RUNTIME: run on the class file RetentionPolicy. SOURCE: In the SOURCE file, the discarded comments SuppressWarnings and Override are RetentionPolicy. SOURCE, Deprecated is in RetentionPolicy. RUNTIME, which must be RetentionPolicy to be the same as the RUNTIME call definition. RUNTIME, RetentionPolicy by default. CLASS: 3. specify the Target format. For example, @ Target (ElementType. the annotation defined by METHOD can be used to annotate members. If this annotation is not declared, it can be placed on any program element. It can be a package, interface, parameter, method, local variable, field.... [Java] // defines that this annotation is kept in the bytecode @ Retention (RetentionPolicy. RUNTIME) @ Target ({ElementType. METHOD, ElementType. TYPE}) // you can define interfaces on methods and classes, public @ interface MyAnnotation {} @ MyAnnotation // annotation class defined by the application public class ApplyMyAnnotation {@ MyAnnotation // defines public static void main (String [] args) on the Method) {if (ApplyMyAnnotation. class. isAnnotationPresent (MyAnnotation. class) {// determine whether the specified annotation class MyAnnotation annotation = (My Annotation) ApplyMyAnnotation. class. getAnnotation (MyAnnotation. class); System. out. println (annotation) ;}} 3. add attribute 1 to the annotation. type annotation attributes can be: 8 basic data types, String, enumeration, annotation, Class, array type, 2. note: when there is only one attribute in the annotation solution or only one attribute needs to be assigned a value, you can directly write data during the call without specifying the attribute name, if the attribute of the annotation is of the array type and only one value is assigned when the value is assigned, You can omit {}. 3. example 3. 1. attribute type (String) [java] import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. *; // defines that this annotation is kept in the bytecode @ Retention (RetentionPolicy. RUNTIME) public @ interface MyAnnotation {String value (); String Color () default "red"; // set the default value to "red"} @ MyAnnotation ("java ") public class ApplyMyAnnotation {public static v Oid main (String [] args) {/*** this is the Annotation on the class, you can also obtain the Annotation on the method, the following uses the annotation obtained on the class as an example */if (ApplyMyAnnotation. class. isAnnotationPresent (MyAnnotation. class) {// determine whether the specified annotation class MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation exists on this class. class. getAnnotation (MyAnnotation. class); System. out. println ("value =" + annotation. value (); System. out. println ("Color =" + annotation. color () ;}}result: value = java Color = red from the called program, you can also It can be seen that if only one attribute can be assigned a value, the attribute name can be omitted. Otherwise @ annotation class (attribute name = value) 3. 2. comprehensive Type [java]/* enumeration class */public enum Week {SUN, MON;}/*** annotation class */public @ interface annotationText {String value ();} import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. *; // defines that this annotation is kept in the bytecode @ Retention (RetentionPolicy. RUNTIME) public @ interface MyAnnotation {String value (); String Color () default "red"; // set the default value to "red" Week week () default Week. MON; // Enumeration type int [] array () default {1, 2, 3}; // array type annotationText annotation () default @ annotationText ("MY "); // annotation Class classDemo () default Integer. class; // Class type} @ MyAnnotation (value = "java", Color = "green", week = Week. SUN, array = 5, annotation = @ annotationText ("YOU"), classDemo = String. class) // array = {4, 5, 6} public class ApplyMyAnnotation {public static void main (String [] args) {/*** this is the Annotation on the class, you can also obtain the Annotation on the method. The following uses the Annotation on the class as an example */if (ApplyMyAnnotation. class. isAnnotationPresent (MyAnnotation. class) {// determine whether the specified annotation class MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation exists on this class. class. getAnnotation (MyAnnotation. class); System. out. println ("value =" + annotation. value (); System. out. println ("Color =" + annotation. color (); System. out. println ("week =" + annotation. week (); System. out. println ("array length =" + annotation. array (). length); System. out. println ("annotation type value =" + annotation. annotation (). value (); System. out. println ("Class type value =" + annotation. classDemo () ;}}result: value = java Color = green week = SUN array length = 1 annotation type value = YOU Class value = classjava. lang. string 4. [java] import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy;/*** annotation class */@ Retention (RetentionPolicy. RUNTIME) public @ interface annotationText {String value ();} public class ApplyMyAnnotation {public static void main (String [] args) throws Exception {Method methodshow = ApplyMyAnnotation. class. getMethod ("show"); annotationText anno = methodshow. getAnnotation (annotationText. class); System. out. println (anno. value () ;}@ annotationText ("java") public void show () {System. out. println ("hello") ;}} result: java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.