scala 自定義實現枚舉

java使用枚舉類型,所以不用
scala但是沒有枚舉類型,如果項目中 需要 用到枚舉或者類似枚舉 。scala 就需要 使用其他方式來實現。
基本來說 有三種
第一種 ,就是在項目中單獨寫一個java文件,在文件中聲明 枚舉類型,然后在其他scala文件中去引入 這個java文件類 ,就可以達到 使用枚舉類型的方法

第二種 ,就是使用 case 類來達到當做枚舉類型的使用,可以參考
https://github.com/ktonga/tweet-virality/blob/12fe9e789234b95a1a337ee887d90e5a021fca92/app/com/github/ktonga/tweetvirality/models/twitter/Neo4j.scala

 case class NeoLabel(name: String) extends Label
  case class NeoRelType(name: String) extends RelationshipType

  val User = NeoLabel("User")
  val Tweet = NeoLabel("Tweet")

  val Follows = NeoRelType("Follows")
  val FollowedBy = NeoRelType("FollowedBy")
  val Twitted = NeoRelType("Twitted")
  val TwittedBy = NeoRelType("TwittedBy")

在調用的時候

 NeoLabel.User
NeoLabel.Tweet

第三種使用 封閉的trait 來實現


 class enum extends scala.annotation.StaticAnnotation;

@enum sealed trait AggregateOp extends scala.Product with scala.Serializable {
    def json: String
  };
  object AggregateOpEnums {
    case object Values extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"values\""
    };
    case object Count extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"count\""
    };
    case object Valid extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"valid\""
    };
    case object Missing extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"missing\""
    };
    case object Distinct extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"distinct\""
    };
    case object Sum extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"sum\""
    };
    case object Mean extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"mean\""
    };
    case object Average extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"average\""
    };
    case object Variance extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"variance\""
    };
    case object Variancep extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"variancep\""
    };
    case object Stdev extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"stdev\""
    };
    case object Stdevp extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"stdevp\""
    };
    case object Median extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"median\""
    };
    case object Q1 extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"q1\""
    };
    case object Q3 extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"q3\""
    };
    case object Modeskew extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"modeskew\""
    };
    case object Min extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"min\""
    };
    case object Max extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"max\""
    };
    case object Argmin extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"argmin\""
    };
    case object Argmax extends AggregateOp with scala.Product with scala.Serializable {
      val json: String = "\"argmax\""
    }
  };
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 如果這個世界上有后悔藥,我相信你不管花什么代價都愿意;如果這個世界上有時光穿梭機,我相信你會毫不猶豫的穿梭過去;如...
    小美2016閱讀 874評論 0 0