Hacker programmer-Java Annotation

Source: Internet
Author: User

-------------------- Android training, Java training, and hope to communicate with you! ----------------------

I learned the annotation today and wrote down my understanding. Let's learn it together.
The first is the four meta annotations of Java. The so-called meta annotation is the annotation of the annotation, that is, the annotation (a bit messy), they are

1. @ target, indicating where the annotation is used. There are eight parameters:

Elementtype. annotation_type (annotation type declaration)

Elementtype. Constructor (constructor)

Elementtype. Field (field Declaration)

Elementtype. local_variable (local variable Declaration)

Elementtype. Method (method declaration)

Elementtype. Package (package Declaration)

Elementtype. parameter (parameter Declaration)

Elementtype. Type (class, interface or enumeration Declaration)

2. @ retension, indicating that the annotation information is saved there. There are three parameters:

Retentionpolicy. class (the annotation information is recorded in the class, and the running is discarded, which is the default)

Retentionpolicy. Runtime (the annotation information is record at runtime, that is, the operation is retained)

Retentionpolicy. Source (the annotation information is recorded in the source file, and compiled into a class file will be discarded)

3. @ brief ented include this annotation in javadoc

4. @ inherited allows subclass to inherit the annotation in the parent class

 

Java has three default annotations, which are

1. @ override, used to tell the compiler that the following method is to override the parent class.

2. @ deprecated, used to tell the compiler that this is an outdated API and give the compiler a warning

3. @ suppresswarnings: Used to tell the compiler to ignore warning information and disable warning information.

 

Next, let's write a custom annotation:

Import Java. lang. annotation. *; @ target (elementtype. method) @ retention (retentionpolicy. runtime) Public @ interface a {string ID (); string color () Default "yellow"; // This uses the default value, which is used when not set .}

Write a class usage Annotation

Public Class B {@ A (ID = "110") // This annotation must be a method, because the above annotation is @ target (elementtype. method) Public void a () {}@ A (ID = "120", color = "red") // public void B () {}} exists during running (){}}

The above is the use of custom annotations. The following example uses reflection to parse annotations.

public class C {public static void main(String[] args) {B b = new B();Method[] ms = b.getClass().getDeclaredMethods();for(Method m:ms){if(m.isAnnotationPresent(A.class)){A a = m.getAnnotation(A.class);System.out.println(m.getName()+":"+a.ID()+":"+a.Color());}}}}

The result is
A: 110: Yellow
B: 120: red

Now I have a preliminary understanding of the annotation, so why should I use the annotation and reference the following example?

public class D extends B{//@Overridepublic void A(){System.out.println("hello");}}

The above example was originally intended to rewrite the () function, but was accidentally written as a () in upper case. If @ override exists, the compiler will generate an error. If it is commented out, then the compiler will think that I am writing a new function, so no error is reported. Conclusion: The annotation is simply for the machine to see, so that the machine can tell us the unintentional error.

 

-------------------- Android training, Java training, and hope to communicate with you! ----------------------
For details, see edu.csdn.net/heima.

 

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.