Java --- pieces

1. Introduction to enumeration classes

  • 1. Understanding of enumerated classes: as long as there are a limited number of objects of the class, certain. We call this class an enumeration class
  • 2. When you need to define a set of constants, it is strongly recommended to use an enumeration class
  • 3. If there is only one object in the enumeration class, it can be used as an implementation of the singleton pattern.

Second, how to define the enumeration class

  • Method one, before jdk5.0, custom enumeration class
  • Method two, jdk5.0, you can use the enum keyword to define the enumeration class
    Method one:
//自定义枚举类 若只有一个对象,可看成单例模式
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("冬天", "冰天雪地");

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

    public String getSeasonDesc() {
        return seasonDesc;
    }
    //4.其他诉2:提供toString方法

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

Three, common methods in enum:

  • Values ​​() method: returns an array of objects of the enumeration type, this method can easily traverse all enumeration values
  • valueOf (String str): You can convert a string from an enumeration class object, requiring that the string must be the "name" of the enumeration class object
  • If not, there will be an exception
  • toString (): Returns the name of the constant of the current enumeration class object

Fourth, the enumeration class defined by the enumeration class keyword implementation interface

  • Case 1: Implement an interface and implement abstract methods in enum
  • Case 2: Let the objects of the enumeration class implement the abstract methods in the interface

Method 2:

/使用enum关键字定义枚举类
//定义的枚举类,默认继承class java.lang.Enum类
enum Season1 implements Info{
    //1.提供当前枚举类的对象,多个对象之间用","隔开,末尾";"结束了,如果连属性的没有直接写常量名即可
//    public static final Season SPRING =new Season("春天","春暖花开");
    //对比
    SPRING("春天", "春暖花开"){
    //重写Info接口的方法
        @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.其他诉求2:提供toString方法,默认打印当前类的信息

    //    @Override
//    public String toString() {
//        return "Season{" +
//                "seasonName='" + seasonName + '\'' +
//                ", seasonDesc='" + seasonDesc + '\'' +
//                '}';
//    }
    public static void main(String[] args) {
        Season1 autumn = Season1.AUTUMN;
        System.out.println(autumn);
            //toString():返回枚举类对象的名称
          System.out.println(autumn.toString());
//        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]);
            //遍历每一个方法,实现各自的show()方法
            System.out.println(values[i]);
            values[i].show();
        }
        System.out.println("***********************");
        //valueOf(String str) 返回枚举类中对象为str的对象
        //如果没有str的枚举类对象,则抛异常 IllegalArgumentException
        Season1 winter = Season1.valueOf("WINTER");
        System.out.println(winter);

    }
//单独重写,诉求让每一个常量都实现不同的方法
//    @Override
//    public void show() {
//        System.out.println("这是一个季节");
//    }
}
interface Info{
    void show();
}
public class SeasonTest {
    public static void main(String[] args) {
        Season autumn = Season.AUTUMN;
        System.out.println(autumn);
    }
Published 19 original articles · liked 0 · visits 486

Guess you like

Origin blog.csdn.net/weixin_43244120/article/details/105333376