在java1.7之前 switch中使用String作为分支条件
来源:互联网 发布:visual basic 知乎 编辑:程序博客网 时间:2024/05/16 06:41
在java中明确规定switch里面只允许使用int,char,和enum类型,但是我们实际编程过程中往往需要通过String来进行比较。那么应该怎么做呢?其实很简单,我们只需要将enum类型做一个小小的封装就可以达到我们的目的
Animal enum类:
1 public enum Animal { 2 dog,cat,bear;3 4 public static Animal getAnimal(String animal){ 5 return valueOf(animal.toLowerCase()); 6 } 7 }
Client 调用:
public class Client { public void caseAnimal(String animal){ switch(Animal.getAnimal(animal)){ case cat: System.out.println("this is a cat"); break; case dog: System.out.println("this is a dog"); break; case bear: System.out.println("this is a bear"); break; } } /** * @param args */ public static void main(String[] args) { Client client = new Client(); client.caseAnimal("cat"); } }
0 0
- 在java1.7之前 switch中使用String作为分支条件
- 在java switch中使用String作为分支条件
- 在java switch中使用String作为分支条件
- 在java switch中使用String作为分支条件
- Java switch中使用string作为分支条件
- Java switch中使用string作为分支条件
- 使用枚举实现switch中使用String作为条件
- java switch的使用+switch用String作为条件
- java中的switch用String作为条件
- java1.7版本之后,switch条件支持字符串
- 在switch分支中使用return可以替代break
- OC中switch使用字符串作为参数
- 分支中switch语句
- 关于 Java中switch条件是否能为string类型
- 在条件判断switch()语句中,可以使用哪些基本类型呢?
- JDK7支持switch中使用String
- JDK7支持switch中使用String
- java switch case 中使用String
- linux小结
- 【ZooKeeper Notes 7】使用super身份对有权限的节点进行操作
- qt截屏模块
- Android中的monkey用法
- XHTML之基础三问
- 在java1.7之前 switch中使用String作为分支条件
- JAVA 跨平台原理
- 九度OJ 1084 整数拆分
- 项目总结—jQuery EasyUI- DataGrid使用
- 敏捷开发之道(四)Scrum概述
- C#的TabControl模拟IE7程序事例,带关闭功能
- FastJson解析
- ZooKeeper概述
- MySQL+Sphinx实现全文搜索