Note Annotation Introduction, and annotations simple application, array type properties, not finished---Enumeration type properties, annotation type properties

Source: Internet
Author: User
Tags deprecated reflection



Package com.base_super.annotation;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;

Import Java.util.Arrays; /** * Annotations: New features of JDK1.5 * Summary: * 1: The annotation is equivalent to a mark, add the annotation in the program is equal to a certain mark for the program, without adding, it is equal to not some kind of mark, later, Javac compiler, development tool * and other program can use reflection to understand your class to and the various elements on the mark, to do the appropriate thing.
 Tags can be added to packages, classes, fields, methods, parameters of methods, and local variables. * 2, look at the Java.lang package, you can see the most basic annotation * * * * @author WJW * */////Class public class AnnotationTest1 {pu for "Apply annotation class" for reflection operation
	Blic static void Main (string[] args) {//method ();//Annotataion_class.sayhello ();//This is the effect of invoking an obsolete method method1 (); ///@SuppressWarnings (value = {"Deprecation"}) @SuppressWarnings ("deprecation")//To declare that the method is deprecated.
	So that it doesn't have to be underlined again, but myeclipse this feature does not do well and is showing public static void method () {System.runfinalizersonexit (true); The public static void Method1 () {//Determines whether the class applying the annotation is an applied zidingyi_annotation_class this annotation boolean boo=annotataion_class.class.i Sannotationpresent (zidingyI_annotation_class.class);
			if (boo) {Zidingyi_annotation_class zdy=annotataion_class.class.getannotation (zidingyi_annotation_class.class);
			System.out.println (Zdy);
			System.out.println ("dddddddddd"); System.out.println (Zdy.color ());//Call that property method System.out.println (Zdy.value ());//Call that property method, which can be called as normal as other properties//System.out . println (Arrays.aslist, Zdy. Arrayarr ())//The reshaping array cannot be printed through the Arrays.aslist () method because int[], not object[] System.out.println (zdy. Arrayarr (). length);//Here is the size of the print array}}/** * Apply the annotation class * @author ZJW *//////////@ZiDingYi_Annotation_class// On the class//@ZiDingYi_Annotation_class (color = "Yellow", value= "AAAAAAAAAA")//Because the attribute method is added to the annotation, the property is also initialized//@ZiDingYi_ Annotation_class (value= "aaaaaaaaaa")//Because the attribute method is added to the annotation, the attribute is also initialized//@ZiDingYi_Annotation_class (value= "BBB", arrayarr={1,2,3,4,5,6})//You can assign a new value to a property that has already been assigned @ZiDingYi_Annotation_class (value= "CC", arrayarr=888)//If the array has only one value, You can write it straight, you don't have to write curly braces. Class annotataion_class{@Deprecated//The annotation indicates that the method is obsolete and recommends that the method not be used, but the method can still call the public staticvoid SayHello () {System.out.println ("hi,jack!!!!");
	@Override public String toString () {return "@Override the effect of this annotation is to ensure that the method you write overrides the method of the parent class"; }/** * Custom annotation class * @author ZJW * * * *//META information: information of the message and so on//This is a meta annotation, is the annotation annotation/** * @Retention meta annotation, three kinds of values: * retetionpolicy.so Urce---Corresponds to-----java source file @Override, @SuppressWarnings are applied at this stage * retetionpolicy.class----corresponding-----CLASS file, which is the default phase * Rete tionpolicy.runtime--corresponds to-----in-memory byte code @Deprecated applied at this stage */@Retention (value = retentionpolicy.runtime) @Targe T ({Elementtype.method,elementtype.type})//This adds the annotation so that the custom annotation can only be applied to the corresponding position, this setting is applied to the method, and the class, Note that the Elementtype.method representation is applied to the method, and the Elementtype.type representation is applied to the class//As to why type is substituted for class, because class implements the type interface, and multiple types such as: enum,@ interface) and so on implements the type interface @interface zidingyi_annotation_class{//String color ();//Add Property method in note string color () default "blue" ";//Add a property method to the annotation and set the default value, at which point you can think that only the following value is assigned a property, which can be abbreviated as: @ZiDingYi_Annotation_class (value=" aaaaaaaaaa ") String Value ()//Note This property is very special, if only this property needs to be assigned, when adding annotations, can be abbreviated as: @ZiDingYi_AnnOtation_class (value= "aaaaaaaaaa") int[] Arrayarr () default {2,3,4};//Define shaping array property methods}
 


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.