[Java enumeration class] The enumeration class defined by the enum keyword implements the interface

Enum class that implements the interface

1. Like ordinary Java classes, enumeration classes can implement one or more interfaces

2. If each enumeration value exhibits the same behavior when calling the implemented interface method, it is only necessary to implement the method uniformly.

3. If you need each enumeration value to behave differently when calling the implemented interface method, you can let each enumeration value implement the method separately

=========================================================================

Case 1: implement the interface and implement the abstract method in the enum class
public class SeasonTest01 {
    public static void main(String[] args) {
        Season1 winter = Season1.valueOf("WINTER");
        System.out.println(winter);
        winter.show();
    }
}
interface Info{
    void show();
}
//使用enum关键词枚举类
enum Season1 implements Info{
    //1.提供当前枚举类的多个对象,多个对象之间用“,“隔开,末尾对象";"结束
    SPRING ("春天","春暖花开"),
    SUMMER ("夏天","夏日炎炎"),
    AUTUMN ("秋天","秋高气爽"),
    WINTER ("冬天","冰天雪地");


    //2.声明Season对象的属性:private final修饰
    private final String seasonName;
    private final String seasonDesc;

    //3.私有化类的构造器,并给对象属性赋值
    private Season1(String seasonName,String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }


    //4.其他诉求1:获取枚举类对象的属性
    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }

    @Override
    public void show() {
        System.out.println("这是一个季节");
    }

    //4.其他诉求1:提供toString()
//    @Override
//    public String toString() {
//        return "Season1{" +
//                "seasonName='" + seasonName + '\'' +
//                ", seasonDesc='" + seasonDesc + '\'' +
//                '}';
//    }
}

If we want the show() called by each object to display different content

Case 2: Let the objects of the enumeration class implement the abstract methods in the interface respectively

public class SeasonTest01 {
    public static void main(String[] args) {
        Season1 summer = Season1.SUMMER;
        //toString():
        System.out.println(summer);

        //System.out.println(Season1.class.getSuperclass());

        System.out.println("===================================");
        //values():
        Season1[] values = Season1.values();
        for (int i = 0; i < values.length; i++) {
            System.out.println(values[i]);
            values[i].show();
        }

        System.out.println("=======================================");
        //valuesOf(String objName):返回枚举类中对象名是objName的对象
        //如果没有objName的枚举类对象,则抛异常:IllegalArgumentException
        Season1 winter = Season1.valueOf("WINTER");//WINTER1报错
        System.out.println(winter);
        winter.show();
    }
}
interface Info{
    void show();
}
//使用enum关键词枚举类
enum Season1 implements Info{
    //1.提供当前枚举类的多个对象,多个对象之间用“,“隔开,末尾对象";"结束
    SPRING ("春天","春暖花开"){
        @Override
        public void show() {
            System.out.println("春天在哪里");
        }
    },
    SUMMER ("夏天","夏日炎炎"){
        @Override
        public void show() {
            System.out.println("宁静的夏天");
        }
    },
    AUTUMN ("秋天","秋高气爽"){
        @Override
        public void show() {
            System.out.println("秋天不回来");
        }
    },
    WINTER ("冬天","冰天雪地"){
        @Override
        public void show() {
            System.out.println("大约在冬季");
        }
    };


    //2.声明Season对象的属性:private final修饰
    private final String seasonName;
    private final String seasonDesc;

    //3.私有化类的构造器,并给对象属性赋值
    private Season1(String seasonName,String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }


    //4.其他诉求1:获取枚举类对象的属性
    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }


    //4.其他诉求1:提供toString()
//    @Override
//    public String toString() {
//        return "Season1{" +
//                "seasonName='" + seasonName + '\'' +
//                ", seasonDesc='" + seasonDesc + '\'' +
//                '}';
//    }
}

The result of the operation is as follows:

thanks for watching! !

Guess you like

Origin blog.csdn.net/qq_64976935/article/details/129540991