【发布时间】:2020-05-15 20:04:18
【问题描述】:
给定以下代码:
public class Practice1 {
public enum Dogs {collie, harrier};
public static void main(String[] args) {
Dogs myDog = Dogs.collie;
switch (myDog) {
case collie:
System.out.println("collie ");
case harrier:
System.out.println("harrier ");
}
}
}
输出是
collie
harrier
为什么当 Dogs myDog = Dogs.collie 时它会打印出这两种情况? 我意识到没有休息,但它不应该仍然只打印 collie 吗?
此外,使用 Dogs myDog = Dogs.harrier 它只打印 harrier
【问题讨论】:
-
这就是switch语句的工作方式。见docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html。
标签: enums switch-statement instantiation