Java learning road-enumeration class

Java learning road-enumeration class

Overview

In mathematics and computer science theory, an enumeration of a set is a program that lists all the members of some finite sequence set, or a count of objects of a specific type. These two types often overlap. --Wikipedia

Enumerated type is part of the new features in Java 5, it is a special data type.

Its special feature is that it is a class type but has more special constraints than ordinary class types, but the existence of these constraints also creates the simplicity, safety and convenience of enumeration types.

The enumeration class has the following characteristics:

  • The defined enumtype is always inherited from java.lang.Enumand cannot be inherited;
  • enumInstances that can only be defined , but cannot be newcreated by operators enum;
  • Each instance defined is the only instance of the reference type;
  • You can use enumtypes for switchstatements.

One, create an enumeration class

In Java programs, using the enumeration class enumkeyword to define, use a comma between the various constants defined , to split.

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Weekday.SUN);
    }
}

enum Weekday {
    
    
    SUN, MON, TUE, WED, THU, FRI, SAT;
}

2. Common operations

1. name()Method

Returns the string form of the constant name.

System.out.println(Weekday.SUN.name());  // SUN

2. ordinal()Method

Returns the index value of the constant.

In the enumeration class, each constant has an index value like an array, and the index value is related to the position of the constant. The index value of changing the order of the constant will also change.

System.out.println(Weekday.SUN.ordinal());  // 0

3. valueOf()Method

Returns the enum constant of the specified string value.

System.out.println(Weekday.valueOf("SUN"));  // SUN 

4. Traverse

We can experiment forstatement to traverse the enumeration constants class.

for (Weekday var : Weekday.values()) {
    
    
	System.out.print(var + " ");
}  // SUN MON TUE WED THU FRI SAT

5. switchUse in statements

Scanner scan = new Scanner(System.in);

Weekday day = Weekday.valueOf(scan.nextLine());
switch (day) {
    
    
    case SUN:
        System.out.println("SUN");
        break;
    case MON:
        System.out.println("MON");
        break;
    default:
    	System.out.println("Others");
}

Three, advanced use

What type of constants are in the enumeration class?

Each constant in the enumeration class is actually an instance of the enumeration class.

System.out.println(Weekday.SUN.getClass());  // class Weekday

Since every constant is an instance of the enumeration class, the methods and properties defined in the enumeration class should also be applied to every instance.

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Weekday.SUN.getChinese());  // 星期天
    }
}

enum Weekday {
    
    
    SUN("星期天", 7), MON("星期一", 1), TUE("星期二", 2),
    WED("星期bai三", 3), THU("星期四", 4), FRI("星期五", 5),
    SAT("星期六", 6);

    private String chinese;
    private int day;

    Weekday(String name , int day) {
    
    
        this.chinese = name;
        this.day = day;
    }

    public String getChinese() {
    
    
        return chinese;
    }
}

It turns out that it is feasible, so we can expand the use of our constants by adding attributes and methods to the enumeration class!

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/112724489