Java Basic Enumeration Study Notes

1. What is an enumeration?

An enumeration is a collection of constants.
It can be understood in this way: enumeration belongs to a special class, which only contains a limited set of specific objects.
For example, if there is an object of seasons, and the seasons are only four seasons of spring, summer, autumn and winter and cannot be modified, then our traditional
way of creating objects cannot be realized. Therefore, such a design is not good, so we need an object Enumerating classes
Enumerating classes is to list specific objects one by one.
Examples in the traditional way:

public class Enumeration {
    
    
    public static void main(String[] args) {
    
    
        Season spring = new Season("春天","温暖");
        Season sunmer = new Season("夏天","炎热");
        Season autumn = new Season("秋天","凉爽");
        Season winter = new Season("冬天","温暖");
        Season xxxxxx = new Season("xx天","xxxx");//可以看到我们用传统的方式还能添加无关的信息
        sunmer.setName("设置为红天");    //并且可以对原有的属性进行修改
    }
}
class Season{
    
    
    private String name;
    private String desc;
    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    public String getDesc() {
    
    
        return desc;
    }
    public void setDesc(String desc) {
    
    
        this.desc = desc;
    }
    public Season(String name, String desc) {
    
    
        this.name = name;
        this.desc = desc;
    }
}

2. The way to realize enumeration

1. Custom implementation enumeration

four characteristics

1) Constructor privatization (do not allow external creation of objects)
2) Create a set of objects [ ] inside this class (list fixed objects)
3) Externally expose objects (by adding public final static modifiers to objects) (allow outsiders to This object is accessed)
4) You can provide a get method, but do not provide a set (read-only)
use case:

public class Enumeration2 {
    
    
    public static void main(String[] args) {
    
    
    		//类加载创建自动创建静态变量
        System.out.println(Season2.AUTUMN);
    }
}
class Season2{
    
    
    private String name;
    private String desc;
    //通过final修饰的变量一般都大写
    //定义一组不能被修改的对象
    public final static Season2 SPTING = new Season2("春天","温暖");
    public final static Season2 SUMMER = new Season2("夏天","炎热");
    public final static Season2 AUTUMN = new Season2("秋天","凉爽");
    public final static Season2 WINTER = new Season2("冬天","寒冷");
    //去除set()方法
    public String getName() {
    
    
        return name;
    }
    public String getDesc() {
    
    
        return desc;
    }
    //构造器私有化
    private Season2(String name, String desc) {
    
    
        this.name = name;
        this.desc = desc;
    }
    @Override
    public String toString() {
    
    
        return "Season2{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}

2. Use the enum keyword to implement enumeration

(equivalent to a simplified version of the custom enumeration class)

There are five details of using the enum keyword

1. Use the keyword enum instead of class (after using this keyword, it will implicitly inherit the Enum class by default)
2. public static final Season SPRING = new Season("spring", "warm")
directly use SPRING("spring" , "warm")->constant name (actual parameter list)
3. If enum is used to implement enumeration, it is required to define the constant object and write it in front.
4. If there are multiple constants (objects), use the number interval , the last one has a semicolon
5. If the object is created using a no-argument constructor, the parentheses can also be omitted
Use cases:

public class Enumeration3 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Season3.AUTUMN);
    }
}
//class被替换成enum
enum Season3{
    
    
    //写在了第一行,并且多个常量(对象)用,隔开     SPRING("春天","温暖")-->常量名(实参列表)    What是调用了无参构造器
    SPRING("春天","温暖"),SUMMER("夏天","炎热"),AUTUMN("秋天","凉爽"),WINTER("冬天","寒冷"),What;
    private String name;
    private String desc;
    public String getName() {
    
    
        return name;
    }
    public String getDesc() {
    
    
        return desc;
    }
    private Season3(String name, String desc) {
    
    
        this.name = name;
        this.desc = desc;
    }
    @Override
    public String toString() {
    
    
        return "Season2{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}

Compared with custom enumeration classes, it simplifies the way of defining objects! (so this is generally used)

3. Common methods of Enum

After using this keyword, it will implicitly inherit the Enum class by default, so we can use the methods in the Enum class.
Demo case:

public class EnumMethod {
    
    
    public static void main(String[] args) {
    
    
        //使用Season3枚举类,来演示各种方法(同包下内部类可访问)
        Season3 autumn = Season3.AUTUMN;
        //返回枚举对象的变量名(Enum类重写了toString())
        System.out.println(autumn.toString());  //AUTUMN

        //输出枚举对象的名字
        System.out.println(autumn.name());   //AUTUMN

        //ordinal()输出的是该枚举对象的次序/编号,从0开始编号
        //AUTUMN 枚举对象是第三个,因此输出2
        System.out.println(autumn.ordinal());   //2

        //含有定义的所有枚举对象
        Season3[] values = Season3.values();
        for (Season3 season: values) {
    
    
            System.out.println(season);         //输出每一个枚举对象的信息
        }

        //1.根据你输入的 "AUTUMN "到Season2的枚举对象去查找
        //2.如果找到了,就返回这个对象的具体信息,如果没有找到,就报错
        Season3 autumn1 = Season3.valueOf("AUTUMN");
        System.out.println(autumn1); //输出这个对象的具体信息,autumn==autumn1

        //返回AUTUMN编号-SUMMER编号的值   如果返回0表示同一个对象
        System.out.println(Season3.AUTUMN.compareTo(Season3.SUMMER));
    }
}

4. Enumeration class implements interface

  • After using the enum keyword, you can no longer inherit other classes, because enum will implicitly inherit Enum (equivalent to having a parent class), and Java is a single inheritance mechanism.
  • Enumeration classes are the same as ordinary classes, and can implement interfaces—> enum class name implements interface 1, interface 2 { }
    Use cases:
public class Demo {
    
    
    public static void main(String[] args) {
    
    
        //枚举类不能被实例化(不能被new),必须先调用构造器才能使用里面的方法
        Music.CLASSICMUSIC.playing();
    }
}
interface IPlaying{
    
    
    public  void playing();
}
enum Music implements IPlaying{
    
    
    CLASSICMUSIC;
    @Override
    public void playing() {
    
    
        System.out.println("bbbbb");
    }
}

my learning address

Guess you like

Origin blog.csdn.net/cc1373709409/article/details/123073011