Java Learning Series (21) Java object-oriented explanation, java object-oriented

Source: Internet
Author: User

Java Learning Series (21) Java object-oriented explanation, java object-oriented


Reprinted please indicate the source: http://blog.csdn.net/lhy_ycu/article/details/45295947


I. Preface

Annotation in Java is used in many aspects, such as @ Test, Spring, SpringMVC, and other frameworks in unit tests. Java has some built-in annotations, such as @ Override (The subclass must overwrite the corresponding method of the parent class) and @ Deprecated (indicating that the method is not recommended) @ SuppressWarnings (ALARM suppression) and so on. Of course, we can also customize some annotations we need (usually we can add them to the package, class, field, method, method parameters, and local variables ), this requires four meta annotations provided by Java to complete our custom annotation function. In general, annotations are relatively simple. The following describes the four metadata Annotations:


Ii. Four meta annotations (Source Network)

(1) @ Target
@ Target indicates where the annotation is used. The optional values include:
ElemenetType. CONSTRUCTOR Declaration
ElemenetType. FIELD Declaration (including enum instances)
ElemenetType. LOCAL_VARIABLE local variable Declaration
ElemenetType. METHOD declaration
ElemenetType. PACKAGE Declaration
ElemenetType. PARAMETER Declaration
ElemenetType. TYPE class, interface (including annotation TYPE) or enum Declaration
ElementType. ANNOTATION_TYPE Annotation
 
(2) @ Retention
@ Retention indicates the level at which the annotation information is saved. Optional RetentionPolicy parameters include:
The RetentionPolicy. SOURCE annotation will be discarded by the compiler.
The RetentionPolicy. CLASS annotation is available in the class file, but is discarded by the VM.
RetentionPolicy. runtime jvm will also retain annotations at RUNTIME, so the annotation information can be read through the reflection mechanism.

(3) @ brief ented
@ Documented include this annotation in javadoc

(4) @ Inherited
@ Inherited allows subclass to inherit the annotation in the parent class


Iii. instance description 1) Annotation on the role class
// The compiler records the annotation information in the class file. When a Java program is running, the JVM retains the annotation. Therefore, the annotation information can be read through the reflection mechanism. @ Retention (RetentionPolicy. RUNTIME) // The annotation works on the class @ Target (ElementType. TYPE) public @ interface ClassName {// declare the default value String name () default ""; // special value String value ();}

2) Annotations on attributes (fields)
@ Retention (RetentionPolicy. RUNTIME) // The annotation works on the attribute (FIELD) @ Target (ElementType. FIELD) public @ interface FieldName {String value ();}

3) Annotations on Methods
@ Retention (RetentionPolicy. RUNTIME) // comment on the METHOD @ Target (ElementType. METHOD) public @ interface MethodName {String value (); Class <?> Type ();}

4) instance demonstration

Import java. lang. reflect. field; import java. lang. reflect. method; /*** @ author [* Yesterday reproduce *] lhy_ycu@163.com * @ since version 1.0 * @ datetime April 27, 2015 12:57:03 * // specifies which custom annotation class is used, if the name is removed, the default name is: @ ClassName (name = "", value = "") public class User {@ FieldName ("name ") private String name; @ FieldName ("gender") private String sex; public String getName () {return name ;}@ MethodName (type = java. lang. string. CIA Ss, value = "") public void setName (String name) {this. name = name;} public String getSex () {return sex;} @ MethodName (type = java. lang. string. class, value = "male") public void setSex (String sex) {this. sex = sex;} @ Overridepublic String toString () {return "User [name =" + name + ", sex =" + sex + "]";} public static void main (String [] args) throws Exception {Class <?> C1 = Class. forName ("com. leo. annotation. User"); // Class <?> C1 = Student. class; // 1: Get the annotation information on the class // If the annotation of the specified annotation type of the element exists on this object, These annotations are returned, otherwise, nullClassName cn = c1.getAnnotation (ClassName. class); System. out. println (cn. name () + "," + cn. value (); System. out. println ("\ n ============================= "); // 2. Obtain the annotation information Field f1 = c1.getDeclaredField ("sex"); f1.setAccessible (true); FieldName fn = f1.getAnnotation (FieldName. class); System. out. println (fn. value (); // Field [] Fields = c1.getDeclaredFields (); // for (Field field: fields) {// field. setAccessible (true); // FieldName fn = field. getAnnotation (FieldName. class); // if (fn! = Null) {// System. out. println (fn. value (); //} // 3: Obtain the annotation information on the Method [] methods = c1.getDeclaredMethods (); User obj = (User) c1.newInstance (); for (Method method: methods) {System. out. println ("method name:" + method. getName (); // check whether AnnotationMethodName mn = method exists in the method. getAnnotation (MethodName. class); if (mn! = Null) {// callback value assignment. The first parameter is the target object, and the second parameter is the accessed method. invoke (obj, mn. value () ;}} System. out. println (obj. getName () + "," + obj. getSex ());}}

Iv. Summary

1) NOTE: Annotations are not annotations. Annotations provide an important part of implementing program functions, while annotations provide instructions on code functions.

2) annotations allow Java source code to not only contain functional implementation code, but also add metadata.


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.