The Java enum and annotations

Enumeration and notes

enumerate

1. Enumeration

Object enumerated type is limited, a few constants fixed object.

2, syntax

// a form: enum type only constant object list
[modifier] {enum type the name of the enumeration
    constant object list
}

// form II: List only constant object enumerated type
[modifiers] enum enumeration type name {
    const object list;
    other members of the list;
}

Description: const object list must be the first line of the enumeration type

3, how to obtain constant objects enumerated in other classes

// Get a constant object 
enumeration type name. Const object name 
// get a constant object 
enumeration type name .valueOf ( "constant object name") 
// Get all the constant object 
enumeration type name [] all = Enumeration type the name of .values ();
    

4, enumerated types of features

(1) Enumerated types can have a common base of the parent class is java.lang.Enum type, so can not inherit another type

(2) enumerated type constructors must be private

(3) enumerated type may implement an interface

interface MyRunnable{
    void run();
}
enum Gender implements MyRunnable{
    NAN,NV;
    public void run(){
        //...
    }
}
//或
enum Gender implements MyRunnable{
    NAN{
        public void run(){
            //...
        }
    },NV{
        public void run(){
        //...
           }
    };
}

5, the type of the parent class java.lang.Enum

(1) constructor

protected Enum (String name, int ordinal): called automatically by the compiler

(2) String name (): Constant object name

(3) int ordinal (): returns the constant target sequence number, the first number is 0

(4) String toString (): Returns the constant object name, if a subclass want to override the need to manually

(5) int compareTo (Object obj): Comparison of the order of a constant object

annotation

1 comment

It is the code-level comments

 

2, marks: @

 

3, three basic pre-defined annotations:

(1) @Override: indicates that a method is overridden method

It only can be used in the above method, the compiler will check the format of this approach meets the requirements rewrite

(2) @SuppressWarnings (xx): inhibition Warning

(3) @Deprecated: xx represents Obsolete

4, related notes and documentation comments

(1) Documentation Comments

/ **
documentation comment
* /

(2) common documentation comments

@author: Author

@since: join from version xx

@see: Also See

@param: parameter

@return: The return value

@throws or @exception: abnormal

5, JUnit some related notes

(1) @Test: indicates it is a unit test method

This method needs to be: public void xxx () {}

(2) @Before: represents performed before each unit test method

This method needs to be: public void xxx () {}

(3) @After: represents performed after each unit test method

This method needs to be: public void xxx () {}

6, meta-annotation

(1) @Target (xx): It is marked with the annotation can be used in position xx

(Xx): 10 designated by the enumerated type ElementType constant objects, for example: TYPE, METHOD, FIELD, etc.

E.g:

@Target(ElementType.TYPE)

@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})

import static java.lang.annotation.ElementType.*;
@Target({TYPE,METHOD,FIELD})

(2) @Retention (xx): annotation can be labeled with its retention stage to xx

(Xx): is specified by three constant objects RetentionPolicy enumerated type are: SOURCE, CLASS, RUNTIME

Only RUNTIME stage in order to be reflected comment to read

E.g:

@Retention(RetentionPolicy.RUNTIME)

(3) @Documentd: to label the notes may be read into the API

(4) @Inherited: annotation can be labeled with its subclasses inherit

7, custom annotation

@ Membered annotation 
[Modifier] @interface annotation name { 
    
} 
@ element annotation 
[Modifier] {@interface name annotation 
    configuration parameter list 
}

The syntax of the configuration parameters:

The data type parameter name ();

or

The data type parameter name () Default default;

About configuration parameters:

(1) required by the type of configuration parameters:

Eight basic data types, String, enumeration, Class type, annotation, and their array.

(2) If the custom annotation declares that the configuration parameters, you must configure the parameter assignment when using this annotation, unless it has a default value

@ Custom annotation name (name configuration parameter value = 1, the configuration parameter name = value 2 ......)

// if the configuration parameter type is an array, then the assignment can be represented by an array {}
@ custom annotation name (the name of a configuration parameter value = {}, the configuration parameter name = value 2 ......)

(3) If there is only one configuration parameter, a name and value, may be omitted when the value assignment =

(4) If you read the comments, to get the value of configuration parameters, you can access the same method as

Custom annotation object configuration parameters ();

Guess you like

Origin blog.csdn.net/Brevity6/article/details/91408954