Java 中的 Switch 都支持 String 了,為什么不支持 long?
點(diǎn)擊上方 好好學(xué)java ,選擇 星標(biāo) 公眾號(hào)
重磅資訊,干貨,第一時(shí)間送達(dá)
今日推薦:推薦19個(gè)github超牛逼項(xiàng)目!
個(gè)人原創(chuàng)100W +訪問量博客:點(diǎn)擊前往,查看更多
1
結(jié)論
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;
}
}

推薦文章
更多項(xiàng)目源碼
評(píng)論
圖片
表情
