首页 > 代码库 > 枚举类
枚举类
public enum EnumSeason { SPRING, SUMMER, FALL, WINTER; }
public class EnumTest { public static void main(String[] args) { //枚举类默认有一个values()方法,返回该枚举类的所有实例 for(EnumSeason s : EnumSeason.values()) { System.out.println(s); } //使用枚举实例时,可通过EnumClass.variable形式来访问 new EnumTest().judge(EnumSeason.SPRING); } public void judge(EnumSeason s) { //switch语句里的表达式可以是枚举值 switch (s) { case SPRING: System.out.println("春暖花开,正好踏青"); break; case SUMMER: System.out.println("夏日炎炎,适合游泳"); break; case FALL: System.out.println("秋高气爽,进补及时"); break; case WINTER: System.out.println("冬日雪飘,围炉赏雪"); break; } } }
public enum Gender { MALE, FEMALE; public String name; }
public class GenderTest { public static void main(String[] args) { // 通过Enum的valueOf()方法来获取指定枚举类的枚举值 Gender g = Enum.valueOf(Gender.class, "FEMALE"); g.name = "女"; System.out.println(g + "代表" + g.name); } }
public enum Gender { MALE, FEMALE; private String name; public void setName(String name) { switch (this) { case MALE: if (name.equals("男")) { this.name = name; } else { System.out.println("参数错误"); return; } case FEMALE: if (name.equals("女")) { this.name = name; } else { System.out.println("参数错误"); return; } break; } } public String getName() { return this.name; } }
public class GenderTest { public static void main(String[] args) { // 通过Enum的valueOf()方法来获取指定枚举类的枚举值 Gender g = Enum.valueOf(Gender.class, "FEMALE"); g.setName("女"); System.out.println(g + "代表" + g.getName()); g.setName("男"); System.out.println(g + "代表" + g.getName()); } }
FEMALE代表女
参数错误
FEMALE代表女
public enum Gender { MALE("男"), FEMALE("女"); private final String name; private Gender(String name) { this.name = name; } public String getName() { return this.name; } }
public enum GenderImpl implements GenderDesc{ MALE("男"), FEMALE("女"); private final String name; private GenderImpl(String name) { this.name = name; } public String getName() { return this.name; } @Override public void info() { System.out.println("这是个定义性别的枚举类!"); } }
public enum GenderImpl implements GenderDesc{ MALE("男") { //花括号部分实际上是一个类体部分 public void info() { System.out.println("这个枚举值代表男性"); } }, FEMALE("女") { public void info() { System.out.println("这个枚举值代表女性"); } }; private final String name; private GenderImpl(String name) { this.name = name; } public String getName() { return this.name; } @Override public void info() { System.out.println("这是个定义性别的枚举类!"); } }
public enum Operation { PLUS { public double eval(double x, double y) { return x + y; } }, MINUS { public double eval(double x, double y) { return x - y; } }, TIMES { public double eval(double x, double y) { return x * y; } }, DIVIDE { public double eval(double x, double y) { return x / y; } }; public abstract double eval(double x, double y); public static void main(String[] args) { System.out.println(Operation.PLUS.eval(3, 4)); System.out.println(Operation.MINUS.eval(5, 4)); System.out.println(Operation.TIMES.eval(5, 4)); System.out.println(Operation.DIVIDE.eval(5, 4)); } }
7.0 1.0 20.0 1.25
枚举类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。