Home > Java > javaTutorial > Basic usage of Java enumeration classes

Basic usage of Java enumeration classes

PHPz
Release: 2023-05-08 18:04:08
forward
1467 people have browsed it

1. Enumeration rules

  • The objects of the enumeration class can be defined in the class, and new ones are not supported.

  • Enumeration The enumeration class has a constructor, the same as other classes, and can have multiple methods

  • The construction method of the enumeration class must be private, and the member variables can be public or private

Reason: The constructor of the enumeration class can only be called in this class, because other new objects are not supported, so the natural constructor is private

Code example :

package com.meijulei;
//枚举类
public enum Cat {
    A("小明",12),B("小花",22),C("小红",23),D("小蓝",30),E("小绿",20),F("小黄",32);
    public String name;
    private int age;
    private Cat(){
    }
    private Cat(String a,int b){
        name=a;
        age=b;
    }
    public void m1(){
        System.out.println("姓名为:"+name+",年龄为:"+age);
    }
}
//调用
package com.meijulei;
import com.meijulei.Cat;
public class Test {
    public static void main(String[] args) {
        Cat a=Cat.A;
        a.m1();
    }
}
Copy after login

2. Number of enumerations

Note: The number of enumerations refers to the number in the heap, which is the number of objects declared in the enumeration class.

Basic usage of Java enumeration classes

3. Commonly used functions in enumeration classes

**Cat.C.ordinal()**function calculates the position of the object in the enumeration class Which object in the middle (calculated from 0) Cat.C.compareTo(Cat.E) compares the difference between the two objects, subtracting the previous one from the latter one,

A("小明",12),B("小花",22),C("小红",23),D("小蓝",30),E("小绿",20),F("小黄",32);
		System.out.println(Cat.E.ordinal());
        System.out.println(Cat.C.compareTo(Cat.E));
Copy after login

//Output
//4
//-2

4. Implement the enumeration class

Next let’s look at a simple DEMO example:

/**
 * java枚举
 */
public class Enum {
    public static void main(String[] args) {
        System.out.println(Season.SPRING);
        System.out.println(Season.SUMMER);
        System.out.println(Season.AUTUMN);
        System.out.println(Season.WINTER);
    }
}

// 定义的枚举类
enum Season {
    // 枚举定义的常量对象必须在最前面
    SPRING("春天","万物复苏"),
    SUMMER("夏天","烈日炎炎"),
    AUTUMN("秋天","硕果累累"),
    WINTER("冬天","寒冷刺骨");

    private String name;
    private String desc;

    private Season(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public String getDesc() {
        return desc;
    }

    @Override
    public String toString() {
        return "Season{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}
Copy after login

Output:

Season{name='Spring', desc='Revival of All Things'}
Season{name='Summer', desc='Scorching Sun'}
Season {name='Autumn', desc='Fruitful'}
Season{name='Winter', desc='Bitter-cold'}

5. Precautions for using enumeration classes

When we use the enum keyword to develop an enumeration class, the Eunm class will be inherited by default, and it is a final modified class

If we are using a parameterless constructor, Then the parentheses can be omitted directly in the enumeration

// 枚举定义的常量对象必须在最前面
SPRING("春天", "万物复苏"),
SUMMER("夏天", "烈日炎炎"),
AUTUMN("秋天", "硕果累累"),
WINTER("冬天", "寒冷刺骨"),
Other, Shit;

Season() {
}

private Season(String name, String desc) {
    this.name = name;
    this.desc = desc;
}
Copy after login

The above is the detailed content of Basic usage of Java enumeration classes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template