聊一聊Java的枚举enum
- 2019 年 10 月 3 日
- 筆記
?. ?????
?????????????????????????????????????????????????????????????????????????????????????????…????????????????????????????????????????????????????????
?. ????
java???enum???????class?interface????????(???enum????Enum)????????????
???????????????
public enum Quarter { SPRING, SUMMER, AUTUMN, WINTER; }
????????????????private Quarter() {}
public enum Quarter { SPRING, SUMMER, AUTUMN, WINTER; private Quarter(){} }
?????????enum???????private
?????????????????
???????????????????????????????
public enum Quarter { SPRING("?"), SUMMER("?"), AUTUMN("?"), WINTER("?"); private Quarter(String name){ this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } //???? public static void printName(){ System.out.println(Quarter.SUMMER); } //???? public abstract void printValue(); }
?????????????????????????????????????????????????
?????
- ??enum ?????java.lang.Enum????? java.lang.Seriablizable ? java.lang.Comparable ????????????????
- ????????????????public static final ??????enum????????????????
- ?????????????????
?. ??????
1. ????????????
public static void main(String[] args) { //?????? System.out.println(Quarter.SPRING); //??toString()??????????String?? System.out.println(Quarter.SPRING.name()); System.out.println(Quarter.SPRING.toString()); //?????? System.out.println(Quarter.SPRING.getName()); }
?????
SPRING SPRING SPRING ?
2. ????
?????????????????????????????????????Quarter.values()
??
public static void main(String[] args) { //??values()????Quarter????? Quarter[] quarters = Quarter.values(); List<String> quarterParam = new ArrayList<>(quarters.length); //??????????List??quarterParam? for (Quarter quarter : quarters) { quarterParam.add(quarter.toString()); } }
3. switch????
?????????????switch?????????
public static void main(String[] args){ Quarter quarter = Quarter.AUTUMN; switch (quarter) { case SPRING: System.out.println("???????" + SPRING); break; case SUMMER: System.out.println("???????" + SUMMER); break; case AUTUMN: System.out.println("???????" + AUTUMN); break; case WINTER: System.out.println("???????" + WINTER); break; default: System.out.println("???"); break; } }
??????
???????AUTUMN
??????????????default????????WINTER??default????????????????????????????null??????????????????????null??switch???null???java.lang.NullPointerException?
4. ????
4.1. ??????
????????????????==????????????????????????????????????????
public static void main(String[] args){ System.out.println(Quarter.AUTUMN == Quarter.AUTUMN); System.out.println(Quarter.AUTUMN == Quarter.WINTER); }
?????
true false
?????equals()?????????????????????? == ?????
public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable { //equals?? public final boolean equals(Object other) { return this==other; } }
4.2. ????
??compareTo??????????????????????????????????????
public static void main(String[] args){ System.out.println(Quarter.AUTUMN.compareTo(Quarter.SPRING)); System.out.println(Quarter.AUTUMN.compareTo(Quarter.WINTER)); }
??????
2 -1
?????compareTo??
public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable { //????????? private final int ordinal; //????compareTo???????????ordinal??? public final int compareTo(E o) { Enum<?> other = (Enum<?>)o; Enum<E> self = this; if (self.getClass() != other.getClass() && self.getDeclaringClass() != other.getDeclaringClass()) throw new ClassCastException(); return self.ordinal - other.ordinal; } }
5. ?????????
public interface WeatherInterface { //???? public String getTemperature(Quarter quarter); }
public enum Quarter implements WeatherInterface{ SPRING("?"), SUMMER("?"), AUTUMN("?"), WINTER("?"); private Quarter(String name){ this.name = name; } private String name; public String getName() { return name; } //???????? @Override public String getTemperature(Quarter quarter) { switch (quarter) { case SPRING: return "??"; case SUMMER: return "?"; case AUTUMN: return "??"; case WINTER: return "??"; default: return "????"; } } }
????????????
public class Pquarter {} //?????? public enum Quarter extends Pquarter {}
6. ?????????
???????????????????????????????????????????????
public interface Weather { enum Quarter implements Weather { SPRING, SUMMER, AUTUMN, WINTER; } enum Temperature implements Weather { MODERATE, HEAT, COOL, COLD } //???????Weather.Quarter.SPRING??? }
?. ??
?????????enum?????????????????????????????????????????????????????????????