public class Type {
public enum TypeEnum {
ZERO,
ONE
}
private TypeEnum type;
public TypeEnum getType() {
return type;
}
public void setType(TypeEnum type) {
this.type = type;
}
public String getTypeString() {
switch (type) {
case ZERO:
return "ZERO";
case ONE:
return "ONE";
default:
return "OTHER";
}
}
}
B.
public class Type {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTypeString() {
switch (type) {
case "0":
return "ZERO";
case "1":
return "ONE";
default:
return "OTHER";
}
}
}
C.
public class Type {
private int type;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getTypeString() {
switch (type) {
case 0:
return "ZERO";
case 1:
return "ONE";
default:
return "OTHER";
}
}
}
D.
public class Type {
private Integer type;
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getTypeString() {
switch (type) {
case 0:
return "ZERO";
case 1:
return "ONE";
default:
return "OTHER";
}
}
}
结果答案是 B 不知道你是否对了~
switch语句 在 java7版本后支持 String类型,目前是不支持 浮点 和 long等表达式…
不知道看到这里你是否已经能解题了…
这道题为什么错 就是因为 如果我传入的type是null 那会造成什么样的后果捏…我们直接看阿里官方的提供的解释吧…不知道你悟了没有 ,反正我是悟了…



