Java 中的 Switch 都支持 String 了,为什么不支持 long?
好好学java
共 16359字,需浏览 33分钟
·
2021-04-30 08:45
点击上方 好好学java ,选择 星标 公众号
重磅资讯,干货,第一时间送达
今日推荐:推荐19个github超牛逼项目!
个人原创100W +访问量博客:点击前往,查看更多
1
结论
2
枚举类型是咋变成 int 类型的?
public enum SexEnum {
MALE(1, "男"),
FEMALE(0, "女");
private int type;
private String name;
SexEnum(int type, String name) {
this.type = type;
this.name = name;
}
}
public enum Sex1Enum {
MALE("男"),
FEMALE("女");
private String name;
Sex1Enum(String name) {
this.name = name;
}
}
public class SwitchTest {
public int enumSwitch(SexEnum sex) {
switch (sex) {
case MALE:
return 1;
case FEMALE:
return 2;
default:
return 3;
}
}
public int enum1Switch(Sex1Enum sex) {
switch (sex) {
case FEMALE:
return 1;
case MALE:
return 2;
default:
return 3;
}
}
}
// SexEnum.class
public enum SexEnum {
MALE(1, "鐢�"),
FEMALE(0, "濂�");
private int type;
private String name;
// $FF: synthetic field
private static final SexEnum[] $VALUES = new SexEnum[]{MALE, FEMALE};
private SexEnum(int var3, String var4) {
this.type = var3;
this.name = var4;
}
}
// Sex1Enum.class
public enum Sex1Enum {
MALE("鐢�"),
FEMALE("濂�");
private String name;
// $FF: synthetic field
private static final Sex1Enum[] $VALUES = new Sex1Enum[]{MALE, FEMALE};
private Sex1Enum(String var3) {
this.name = var3;
}
}
// SwitchTest$1.class
import com.example.express.test.Sex1Enum;
import com.example.express.test.SexEnum;
// $FF: synthetic class
class SwitchTest$1 {
// $FF: synthetic field
static final int[] $SwitchMap$com$example$express$test$SexEnum;
// $FF: synthetic field
static final int[] $SwitchMap$com$example$express$test$Sex1Enum = new int[Sex1Enum.values().length];
static {
try {
$SwitchMap$com$example$express$test$Sex1Enum[Sex1Enum.FEMALE.ordinal()] = 1;
} catch (NoSuchFieldError var4) {
;
}
try {
$SwitchMap$com$example$express$test$Sex1Enum[Sex1Enum.MALE.ordinal()] = 2;
} catch (NoSuchFieldError var3) {
;
}
$SwitchMap$com$example$express$test$SexEnum = new int[SexEnum.values().length];
try {
$SwitchMap$com$example$express$test$SexEnum[SexEnum.MALE.ordinal()] = 1;
} catch (NoSuchFieldError var2) {
;
}
try {
$SwitchMap$com$example$express$test$SexEnum[SexEnum.FEMALE.ordinal()] = 2;
} catch (NoSuchFieldError var1) {
;
}
}
}
// SwitchTest.class
import com.example.express.test.Sex1Enum;
import com.example.express.test.SexEnum;
import com.example.express.test.SwitchTest.1;
public class SwitchTest {
public int enumSwitch(SexEnum var1) {
switch(1.$SwitchMap$com$example$express$test$SexEnum[var1.ordinal()]) {
case 1:
return 1;
case 2:
return 2;
default:
return 3;
}
}
public int enum1Switch(Sex1Enum var1) {
switch(1.$SwitchMap$com$example$express$test$Sex1Enum[var1.ordinal()]) {
case 1:
return 1;
case 2:
return 2;
default:
return 3;
}
}
}
3
String 类型是咋变成 int 类型的?
public int charSwitch(char c) {
switch (c) {
case 'a':
return 1;
case 'b':
return 2;
default:
return Integer.MAX_VALUE;
}
}
public int charSwitch(char var1) {
switch(var1) {
case 97:
return 1;
case 98:
return 2;
default:
return Integer.MAX_VALUE;
}
}
public int stringSwitch(String ss) {
switch (ss) {
case "ABCDEa123abc":
return 1;
case "ABCDFB123abc":
return 2;
case "helloWorld":
return 3;
default:
return Integer.MAX_VALUE;
}
}
public int stringSwitch(String var1) {
byte var3 = -1;
switch(var1.hashCode()) {
case -1554135584:
if(var1.equals("helloWorld")) {
var3 = 2;
}
break;
case 165374702:
if(var1.equals("ABCDFB123abc")) {
var3 = 1;
} else if(var1.equals("ABCDEa123abc")) {
var3 = 0;
}
}
switch(var3) {
case 0:
return 1;
case 1:
return 2;
case 2:
return 3;
default:
return Integer.MAX_VALUE;
}
}
4
它们的包装类型支持吗?
switch (c) {
case 1:
return 1;
case 2:
return 2;
}
return -1;
}
public int integerSwitch(Integer var1) {
switch(var1.intValue()) {
case 1:
return 1;
case 2:
return 2;
default:
return -1;
}
}
推荐文章
更多项目源码
评论