Java self - Classes and Objects enumeration type

Enumerated type

Step 1: predefined constants

Enumeration enum is a special class (or classes) can easily enum defines constants
such as design an enumerated type season, there are four kinds of constants

public enum Season {
    SPRING,SUMMER,AUTUMN,WINTER
}

A common situation is a switch statement, using the enumeration to be judged

Note: Because it is constant, so are generally all uppercase

public class HelloWorld {
    public static void main(String[] args) {
        Season season = Season.SPRING;
        switch (season) {
        case SPRING:
            System.out.println("春天");
            break;
        case SUMMER:
            System.out.println("夏天");
            break;
        case AUTUMN:
            System.out.println("秋天");
            break;
        case WINTER:
            System.out.println("冬天");
            break;
        }
    }
}

public enum Season {
    SPRING,SUMMER,AUTUMN,WINTER
}

Step 2: The benefits of using enumeration

When the switch is assumed to use, instead of using the enumeration, but the use of int, int and the range of not just 1-4, it is possible to take a value between 1-4 exceeded, so that the judgment result is plausible. (Because only four seasons)

But using enumeration, the scope of the wearer can be defined in four among
SPRING, SUMMER, AUTUMN, WINTER

And will not appear strange Season 5

public class HelloWorld {
    public static void main(String[] args) {
        int season = 5;
        switch (season) {
        case 1:
            System.out.println("春天");
            break;
        case 2:
            System.out.println("夏天");
            break;
        case 3:
            System.out.println("秋天");
            break;
        case 4:
            System.out.println("冬天");
            break;
        }
    }
}

Step 3: traverse the Enumeration

With enhanced for loop, you can easily traverse an enumeration of what are constants

public class HelloWorld {
    public static void main(String[] args) {
        for (Season s : Season.values()) {
            System.out.println(s);
        }
    }
}

Exercise : enumeration

(League there are so few Category:
TANK (Tank)
WIZARD (Master)
ASSASSIN (Assassin)
ASSIST (assist)
WARRIOR (melee)
Ranged (remote)
PUSH (advance)
FARMING (playing field)
design of an enumerated type HeroType, use above classification as a constant

Then write a switch statement, the output of each enumeration constants for the Chinese string)

The answer :

package charactor;
 
public enum HeroType {
    TANK, WIZARD, ASSASSIN, ASSIST, WARRIOR, RANGED, PUSH, FARMING
 
}

.

package charactor;
 
public class Hero {
    public String name; // 姓名
 
    public static void main(String[] args) {
 
        HeroType type = HeroType.ASSASSIN;
        switch (type) {
        case TANK:
            System.out.println("坦克");
            break;
        case WIZARD:
            System.out.println("法师");
            break;
        case ASSASSIN:
            System.out.println("刺客");
            break;
        case ASSIST:
            System.out.println("辅助");
            break;
        case WARRIOR:
            System.out.println("近战");
            break;
        case RANGED:
            System.out.println("远程战");
            break;
        case PUSH:
            System.out.println("推进");
            break;
        case FARMING:
            System.out.println("打野");
            break;
        }
    }
}

Guess you like

Origin www.cnblogs.com/jeddzd/p/11484790.html