java的switch语句问题

来源:互联网 发布:淘宝店铺四个钻石 编辑:程序博客网 时间:2024/05/16 05:56

switch参数为int,char等基本类型,case后直接跟常数

public static void fun(int s) {        switch (s) {            case 1:break;        }    }

switch参数为enum类型,case后面跟的要省去类名

enum Type{    INT, BOOLEAN}public static void fun(Type type) {        switch(type){            case INT: break;        }    }

switch参数为String类型(jdk1.7开始支持),case后面可以直接跟字符串常量

public static void fun2(String s) {        switch (s) {            case "aaa": break;        }    }

switch参数为String类型,但是字符串在一个类中统一管理,需要用public static final修饰

class Constant {    public static final String a = "afa";}public static void fun3(String s) {        switch (s) {            case Constant.a: break;        }    }
0 0
原创粉丝点击