栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

阿里云83行代码挑战赛 一道 单选题 给我整麻了

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

阿里云83行代码挑战赛 一道 单选题 给我整麻了

【单选】 以下哪个选项的 getTypeString 函数,没有 bug? A.
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 那会造成什么样的后果捏…
我们直接看阿里官方的提供的解释吧…

不知道你悟了没有 ,反正我是悟了…
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/352113.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号