属性私有化、常量化——private final 修饰
构造器私有化——private 修饰
提供枚举类的多个对象——public static final 修饰
其他诉求
获取枚举类对象的属性
提供toString的方法——因为默认打印地址值
System.out.println(spring);
//输出结果为com.enumdemo.Season@1b6d3586而不是春天
public class SeasonTest {
public static void main(String[] args) {
Season spring = Season.SPRING;//可以直接给spring用常量SPRING对象赋值
System.out.println(spring.toString());
String result = spring.getSeason();
System.out.println(result);
}
}
/*
春天的描述:春暖花开
春天
*/
public class Season {
//属性私有化、常量化
private final String season;
private final String description;
//构造器私有化
private Season (String seasonName, String seasonDesc) {
this.season = seasonName;
this.description = seasonDesc;
}
//实例化对象——公共、静态、常量
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("冬天","冰天雪顶");
//其他诉求1 获取属性——季节名
public String getSeason() {
return season;
}
public String getDescription() {
return description;
}
//其他诉求2 重写打印方法
@Override
public String toString() {
return season+"的描述:" + description;
}
}
通过Enum代替class去定义类
实例化对象要第一个写——其余与自定义枚举类一样
//Enumd定义:
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎");
//自定义 :
public static final Season SPRING = new Season("春天","春暖花开");
public static final Season SUMMER = new Season("夏天","夏日炎炎");
public class Test {
public static void main(String[] args) {
Season1 spring = Season1.SPRING;
//不用重写toString,父类已经重写好了
System.out.println(spring);//SPRING
System.out.println(spring.getSeason());//春天
System.out.println(spring.getDescription());//春暖花开
//说明定义的枚举类继承于父类java.lang.Enum
System.out.println(Season1.class.getSuperclass());//class java.lang.Enum
}
}
package com.enumdemo;
enum Season1 {
//实例化对象
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
//私有化、常量化属性
private final String season;
private final String description;
//私有化构造器
private Season1(String seasonName, String seasonDesc) {
season = seasonName;
description = seasonDesc;
}
//其他诉求1——获取获取属性
public String getSeason() {
return season;
}
public String getDescription() {
return description;
}
}
不重写toString也不会返回地址值,因为父类已经重写好了
public class Test {
public static void main(String[] args) {
//toString()
Season1 spring = Season1.SPRING;
System.out.println(spring);//SPRING
//values()
Season1[] array = Season1.values();
System.out.println(Arrays.toString(array));
//[SPRING, SUMMER, AUTUMN, WINTER]
//value of()
Season1 a = Season1.valueOf("SPRING");
System.out.println(a);//SPRING
}
}
接口在enum类中实现抽象方法
让枚举类的对象分别实现接口中的抽象方法
public class Test {
public static void main(String[] args) {
Season1 winter = Season1.WINTER;
winter.show();
//这是一个季节
}
}
public interface SeasonInfo {
void show();
}
enum Season1 implements SeasonInfo{
//实例化对象
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
//私有化、常量化属性
private final String season;
private final String description;
//私有化构造器
private Season1(String seasonName, String seasonDesc) {
season = seasonName;
description = seasonDesc;
}
//其他诉求1——获取获取属性
public String getSeason() {
return season;
}
public String getDescription() {
return description;
}
@Override
public void show() {
System.out.println("这是一个季节");
}
}
public class Test {
public static void main(String[] args) {
Season1 winter = Season1.WINTER;
winter.show();
//大约在冬季
}
}
public interface SeasonInfo {
void show();
}
enum Season1 implements SeasonInfo{
//实例化对象
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 season;
private final String description;
//私有化构造器
private Season1(String seasonName, String seasonDesc) {
season = seasonName;
description = seasonDesc;
}
//其他诉求1——获取获取属性
public String getSeason() {
return season;
}
public String getDescription() {
return description;
}
}
原文:https://www.cnblogs.com/Sheltonz/p/13825995.html