zoukankan      html  css  js  c++  java
  • Scala枚举

    object EnumDemo {
      def main(args: Array[String]): Unit = {
        //常用方法
        for (v <- WeekDay.values) {
          println(v.id + ":" + v)
        }
        println("最大ID:" + WeekDay.maxId)
        println("是否存在:"+WeekDay.checkExists("星期八"))
        WeekDay.showAll
      }
    }
    
    
    object WeekDay extends Enumeration {
    
      //类型别名,所有枚举对象类型就可以写成WeekDay而不是Value
      type WeekDay = Value
    
      //protected final def Value(name: String): Value = Value(nextId, name)
      //protected class Val(i: Int, name: String) extends Value with Serializable
      //最终调用Val的主构造器。Val是Value的子类,Value是Enumeration的内部类。构建的时候id从0开始增长。
      val Mon = Value("星期一")
      val Tue = Value("星期二")
      val Wed = Value("星期三")
      val Thu = Value("星期四")
      val Fri = Value("星期五")
      val Sat = Value("星期六")
      val Sun = Value("星期天")
    
      def checkExists(day: String) = this.values.exists(_.toString == day)
    
      def isWorkingDay(day: WeekDay) = !(day == Sat || day == Sun)
    
      def showAll = this.values.foreach(println)
    
    }
    

      

  • 相关阅读:
    053-49
    053-3
    053-204
    053-491
    053-205
    053-57
    053-149
    053-47
    053-150
    回答2
  • 原文地址:https://www.cnblogs.com/noyouth/p/12710671.html
Copyright © 2011-2022 走看看