Java进阶知识查漏补缺11

1、自定义枚举类

package com.cjf.Enum;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: Everything
 * Date: 2020-07-03
 * Time: 20:53
 */

//     方式一:JDK5.0前自定义枚举类
//     方式二:JDK5.0后使用enum关键字定义枚举类


//     方式一:JDK5.0前自定义枚举类
public class EnumTest1 {
    public static void main(String[] args) {
        Season spring = Season.SPRING;
        System.out.println(spring);
    }
}
//自定义季节枚举类
class Season{
    //1.声明Season对象的属性:private final
    private final String seasonName;
    private final String seasonDesc;

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


    //3.实例化当前枚举类的多个对象public static final
    public static final Season SPRING = new Season("春天","春暖花开");
    public static final Season SUMMER = new Season("夏天","夏日炎炎");
    public static final Season AUTUMN = new Season("秋天","秋高气爽");
    public static final Season WINTER = new Season("冬天","冰天雪地");

    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }

    @Override
    public String toString() {
        return "Season{" +
                "seasonName='" + seasonName + '\'' +
                ", seasonDesc='" + seasonDesc + '\'' +
                '}';
    }
}

2.enum关键字

package com.cjf.Enum;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: Everything
 * Date: 2020-07-03
 * Time: 21:24
 */

//     方式二:JDK5.0后使用enum关键字定义枚举类
    //定义的枚举类默认继承于java.lang.Enun类

//    三、三个方法

//    四、使用enum关键字定义的枚举类实现接口的情况
//            情况一:实现接口,在enum类 中实现抽象方法
//            情况二:让枚举类的对象分别实现接口中的抽象方法

public class EnumTest2 {
    public static void main(String[] args) {
        Season1 season1 = Season1.SPRING;
        System.out.println(season1);
        //toString方法,返回当前枚举类的名字
        String string = season1.toString();
        System.out.println(string);

        //values()返回的是一个枚举类数组
        Season1[] values = Season1.values();
        for (int i = 0; i < values.length; i++) {
            System.out.println(values[i].toString());
        }

        //valueOf(String Objname),根据提供的Objname返回同名对象
        Season1 season2 = Season1.valueOf("WINTER");
        System.out.println(season2);
        System.out.println("________");
        season2.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("这是冬天哦!");
         }
     };


    private final String seasonName;
    private final String seasonDesc;

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

    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }

//    @Override
//    public void show() {
//        System.out.println("");
//    }

//    @Override
//    public String toString() {
//        return "Season{" +
//                "seasonName='" + seasonName + '\'' +
//                ", seasonDesc='" + seasonDesc + '\'' +
//                '}';
//    }
}

 

posted @ 2020-07-04 00:01  Tsui98'  阅读(158)  评论(0编辑  收藏  举报