Java Basics-Enumerations and annotations

Source: Internet
Author: User

Enum class:
  1. Custom enumeration Classes
1. Provide the properties of the class, declared private final
2. Attribute declared final, initialized in constructor , privatized constructor , guarantees that its object cannot be created outside the class
3. Calling properties through a public method
4. Create an object of an enumeration class: Declare the object of the class as public static final
  2. Keyword Enum definition enum class
Common methods in 1.enum
(1)values (); Returns all objects of the entire enumeration type as an array
Season[] seasons = Season.values ();//Returns an array
(2)valueOf (String name); An incoming parameter is a property name of an enumeration class that returns an object of that property, or an exception is reported
String str = "SPRING";
Season sea = season.valueof (str);//spring ("Spring" and "Spring Blossoms")
 3. How to enable an enumeration class to implement an interface
1. Let the class implement this interface, the object of the class shares the abstract method of the same set of interfaces
2. Having each object of a class implement an abstract method of the interface, and then invoke the overridden abstract method through the object of the class, performs differently.

Enum season{    Spring ("Spring", "springtime"),    SUMMER ("SUMMER", "summer scorching"),    AUTUMN ("AUTUMN", "crisp"),    WINTER ( "Winter", "Snowy");    Private final String Seasonname;    Private final String Seasondesc;    Private Season (String seasonname, String seasondesc) {        this.seasonname = seasonname;        This.seasondesc = Seasondesc;    }    Public String Getseasonname () {        return seasonname;    }    Public String Getseasondesc () {        return seasondesc;    }}    

Note: Annotation
 1.JDK built-in basic annotation type (3)
@Override: Qualifies overriding the parent class method, which can only be used for methods
@Deprecated: Used to indicate that a program element (class, method, and so on) is obsolete
@SuppressWarnings: Suppress Compiler warnings

  2. Custom annotation Types
Take Suppresswarnings as an example to create
3. Annotate annotations (4) (meta-annotations)
Retention
Rarget
Documented
Inherited

Java Basics-Enumerations and annotations

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.