name()); } } public class EnumExample { public static void main(String[] args) { Color color = Color.RED; System.out.println(color); // 输出 RED color.printColor(); // 输出 RED } } 在上述示例中,Color 是一个枚举类型,包含了三个枚举常量:RED、GREEN 和BLUE。这些枚举常量实际上是 ...
3. public enum TypeEnum { 4. VIDEO, AUDIO, TEXT, IMAGE 5. } 1. 2. 3. 4. 5. 6. 上面的Entity类就可以改成这样: [java] 1. package com.fhp.enumexample; 2. 3. public class Entity { 4. 5. private int id; 6. private TypeEnum type; 7. 8. public int getId() { 9. return ...
enum就像一个class ,在编译时已知一组固定的实例。 例如: public class EnumExample { interface SeasonInt { String seasonDuration(); } private enum Season implements SeasonInt { // except the enum constants remaining code looks same as class // enum constants are implicitly public static final we ...
import java.util.EnumMap; enum Weekday { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } public class EnumMapExample { public static void main(String[] args) { EnumMap map = new EnumMap<>(Weekday.class); map.put(Weekday.MONDAY, "星期一"); map.put(Weekday.TUESDAY, ...
在Java中,枚举是Object的一个子类。让我们看看所有枚举的基类,Enum(为简洁起见进行了修改)。 publicabstractclassEnum<EextendsEnum<E>>implementsConstable, Comparable<E>, Serializable {privatefinalString name;publicfinalStringname(){returnname; }privatefinalintordinal;publicfinalintordinal(){returnordinal; ...
Example 1: Java Enum enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE } class Main { public static void main(String[] args) { System.out.println(Size.SMALL); System.out.println(Size.MEDIUM); } } Run Code Output SMALL MEDIUM As we can see from the above example, we use the enum name...
package org.example.enumX; public class Example2 { public staticvoidmain(String[] args) { choseStudent(1); } public staticvoidchoseStudent(intsex){switch(sex){case1: System.out.println("男孩的特征");break;case2: System.out.println("女孩的特征");break;default: ...
public class EnumExample { public static void main(String[] args) { Day[] days = Day.values(); for (Day day : days) { System.out.println(day); } } } 在上面的示例中,我们定义了一个名为Day的枚举类型,它表示一周的天数。然后,在main()方法中,我们使用values()方法获取Day枚举类型的所有值...
packagecom.fhp.enumexample;publicclassEntity{publicstaticfinalintVIDEO=1;//视频publicstaticfinalintAUDIO=2;//音频publicstaticfinalintTEXT=3;//文字publicstaticfinalintIMAGE=4;//图片privateintid;privateinttype;publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicintgetType(){returnty...
enum Season { SPRING, SUMMER, AUTUMN, WINTER } public class EnumExample { public static void main(String[] args) { Season currentSeason = Season.SUMMER; switch(currentSeason) { case SPRING: System.out.println("It is spring season."); break; case SUMMER: System.out.println("It is summer...