zoukankan      html  css  js  c++  java
  • switch 中可以使用字符串当判断条件

    switch语句能否作用在byte上,能否作用在long上,能否作用在String?

       在switchexpr1)中,expr1只能是一个整数表达式或者枚举常量(更大字体),整数表达式可以是int基本类型或Integer包装类型,由于,byte,short,char都可以隐含转换为int,所以,这些类型以及这些类型的包装类型也是可以的。显然,longString类型都不符合switch的语法规定(版本原因),并且不能被隐式转换成int类型,所以,它们不能作用于swtich语句中。

    Java7之前,switch只能支持 byte、short、char、int或者其对应的封装类以及Enum类型。在Java7中已经支持String类型。

    //枚举类型,把要判断的条件先定义成enum

     enum CompassPoint {
          case North
          case South
          case East
          case West
    }

    //使用 Switch 语句来匹配枚举值

    directionToHead = .South
    switch directionToHead {
      case .North:
          println("Lots of planets have a north")
      case .South:
          println("Watch out for penguins")
      case .East:
          println("Where the sun rises")
      case .West:
          println("Where the skies are blue")
    }
    // 输出"Watch out for penguins”

  • 相关阅读:
    rabbitmq集群几个比较好的文章
    rabbitmq集群步骤
    rabbitmq安装
    查找出系统中大于50k 且小于100k 的文件并删除。
    现将文件a.txt 中的所有abc 替换成def
    统计/var/log/下有多少文件
    压缩解压目录结构不能改变
    chkconfig命令
    linux运维必须掌握
    三剑客
  • 原文地址:https://www.cnblogs.com/panpanpan/p/5983048.html
Copyright © 2011-2022 走看看