Java enum enum class record and summarize the learning and use of

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

1. Create a java enum class enumDemo, this enum class to inherit the default java enumeration enum class , so any enumeration class that you create can not be inherited (inherit) any class.
2. In the enumeration class created by, create an instance must be placed on top of the time (because it is the people sites), to create multiple instances, between instances separated by commas. If the instantiation constant is not initialized, the system will have a default constructor with no arguments private.
SeasonEnum enum {public
// must be placed on top
SPRING, SUMMER, AUTUMN, WINTER;
}
3. enumeration class establishment, if instantiated initialization parameters constant, you must provide the appropriate type of variable parameters, and to provide a private (private) constructor has the Senate , private can not write, default is private.
SUNDAY ( "Sun", 0),
MONDAY ( "Mon",. 1),
TUESDAY ( "TUS", 2),
WEDNESDAY ( "Wed",. 3),
THURSDAY ( "Thu",. 4),
FRIDAY ( "Fri" ,. 5),
SATURDAY ( "SAT",. 6);

private String name;
private int index;

private SeasonEnum2(String name,int index){
    this.name = name;
    this.index = index;
}
SPRING("春天"),SUMMER("夏天"),AUTUMN("秋天"),WINTER("冬天");

private final String seasonName;
SeasonEnum(String seasonName) {
    this.seasonName = seasonName;
}

public String getSeasonName(){
    return seasonName;
}

4. If there is an abstract class enumeration method established in the instance of constants must implement this method.
public enum SeaaonEnum3 {

PLUS{
    @Override
    public int evil(int a, int b) {
        return a + b;
    }
},
MINUS{
    @Override
    public int evil(int a, int b) {
        return a - b;
    }
},
TIMES{
    @Override
    public int evil(int a, int b) {
        return a * b;
    }
},
DIVIDES{
    @Override
    public int evil(int a, int b) {
        return a/b;
    }
};
public abstract int evil(int a,int b);

}
The class may also have other normal methods and variables.

Guess you like

Origin blog.csdn.net/weixin_40358672/article/details/92132379