And then brother combed Java Knowledge points-generics and annotations (14)

Source: Internet
Author: User

enum  season{Spring ("Spring", "Springtime"), SUMMER ("SUMMER", "summer scorching"),AUTUMN ("AUTUMN", "crisp"), WINTER ("WINTER", "Snowy");}

In fact, the above equals to:

classseason{   Public static final SeasonSPRING = new Season("Spring", "Spring Blossoms");   Public static final SeasonSummer= new Season("Summer", "Summer scorching");   Public static final Seasonautumn= new Season("Autumn", "crisp");   Public static final SeasonWinter= new Season("Winter", "snowy"); Private FinalString Seasonname; PriveteFinalString Seasondesc; PrivateSeason (String seasonname,string seasondesc) { This. Seasonname =Seasonname;  This. Seasondesc =Seasondesc; }}

The above actually I wrote an enumeration class, that is, to remove the same, found it? Is the encapsulated enumeration class and then the semicolon at the end of each line is changed to a comma.

Main methods:
①values (): Gets all enumerated class objects, returns an array of objects
season[] Season = Season.values ();
②valueof (String name): Gets the object name of an enumeration class object, returns an enumeration
Season sea = season.valueof ("SPRING");
Note: The parameter spring is an instantiated object name, and when written incorrectly, sea is not null and will error.

How to make an enumeration implement an interface:

iterface info{voidshow ();}enumSeason Implements info{SPRING ("Spring", "Spring Blossoms") { Public voidShow () {System.out.printLn ("Where is Spring?" "); }},summer ("Summer", "Summer scorching") { Public voidShow () {System.out.printLn ("Where is Spring?" "); }},autumn ("Autumn", "crisp") { Public voidShow () {System.out.printLn ("Where is Spring?" "); }},winter ("Winter", "snowy") { Public voidShow () {System.out.printLn ("Where is Spring?" "); }};
}

Summarize:
Discover whether the interface can be overridden within each object, so that objects of different enum classes
The implementation of the interface effect is not the same.

Java annotations
①: Starting with java5.0, added support for metadata, also known as annotations (Annotation).
is a special token of the code that can be read during compilation, class loading, runtime, and processed accordingly. By using annotation, programmers can embed some supplemental information in the source file without changing the original logic.
②annotation can be used to decorate packages, classes, constructors, methods, member variables, parameters, local variables
The three common basic annotation types provided by ③JDK
√ @Override: Overriding the parent class method, only for methods
This does not write, in fact, can be rewritten, the advantage is that the code can be seen at a glance is rewritten
√ @Deprecated: Used to indicate that a program element is obsolete (class, method)
Outdated, does not mean that can not be used, but not recommended.
Each new version of the JDK may go out of date, in fact, to tell you that there is a better
Replace it, try not to use it, maybe the next version we removed.
Add a horizontal line to the class or method.
√ @SupperessWarnings: Suppress compiler warnings
For people with obsessive-compulsive disorder, hehe. Always see some small yellow exclamation mark on the left side of IDE environment

Plus no exclamation mark, Rawtypes said no use of generic unused means useless
④ How to customize annotations
Format:
Public @interface myannotation{
}
Basically no one to use, know the format on the line
⑤ yuan Annotations
@Target ({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR})
@Retention (Retentionpolicy.source)
Public @interface Suppresswarnings {
String[] Value ();
}
The green is the meta-annotation, meaning to modify the annotations
@Retention: Represents the life cycle of the following annotations
Retentionpolicy.source is an enumeration
The delegate is discarded directly at compile time, and is also the default value
@Target: Used to indicate which types can be decorated
For example: I just want annotations to decorate classes and methods, etc.
@Documented: Annotations that specify adornments can be extracted as documents by the Javadoc tool
@Inherited: Annotations that are modified by it are inherited and rarely used. You know, that's fine.

And then brother combed Java Knowledge points-generics and annotations (14)

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.