日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Scala教程之:Enumeration

發布時間:2024/2/28 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Scala教程之:Enumeration 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Enumeration應該算是程序語言里面比較通用的一個類型,在scala中也存在這樣的類型, 我們看下Enumeration的定義:

abstract class Enumeration (initial: Int) extends Serializable

Enumeration是一個抽象類,它定義四個value方法,來設置內部的值, 四個value方法如下定義:

/** Creates a fresh value, part of this enumeration. */protected final def Value: Value = Value(nextId)/** Creates a fresh value, part of this enumeration, identified by the* integer `i`.** @param i An integer that identifies this value at run-time. It must be* unique amongst all values of the enumeration.* @return Fresh value identified by `i`.*/protected final def Value(i: Int): Value = Value(i, nextNameOrNull)/** Creates a fresh value, part of this enumeration, called `name`.** @param name A human-readable name for that value.* @return Fresh value called `name`.*/protected final def Value(name: String): Value = Value(nextId, name)/** Creates a fresh value, part of this enumeration, called `name`* and identified by the integer `i`.** @param i An integer that identifies this value at run-time. It must be* unique amongst all values of the enumeration.* @param name A human-readable name for that value.* @return Fresh value with the provided identifier `i` and name `name`.*/protected final def Value(i: Int, name: String): Value = new Val(i, name)

知道如何設置Enum的值后,我們就可以嘗試創建一個Enum了。

println("Step 1: How to create an enumeration") object Donut extends Enumeration { type Donut = Valueval Glazed = Value("Glazed") val Strawberry = Value("Strawberry") val Plain = Value("Plain") val Vanilla = Value("Vanilla") }

上面的例子中,我們創建了一個Enum,并且設置了幾個值。

下面我們看下怎么取到Enum的值:

println("\nStep 2: How to print the String value of the enumeration") println(s"Vanilla Donut string value = ${Donut.Vanilla}")

你可以看到如下的輸出:

Step 2: How to print the String value of the enumeration Vanilla Donut string value = Vanilla

下面是怎么輸出Enum的id:

println("\nStep 3: How to print the id of the enumeration") println(s"Vanilla Donut's id = ${Donut.Vanilla.id}")

結果如下:

Step 3: How to print the id of the enumeration Vanilla Donut's id = 3

怎么輸出所有的Enum項呢?

println("\nStep 4: How to print all the values listed in Enumeration") println(s"Donut types = ${Donut.values}")

輸出結果如下:

Step 4: How to print all the values listed in Enumeration Donut types = Donut.ValueSet(Glazed, Strawberry, Plain, Vanilla)

接下來,我們看下怎么打印出所有的Enum:

println("\nStep 5: How to pattern match on enumeration values") Donut.values.foreach {case d if (d == Donut.Strawberry || d == Donut.Glazed) => println(s"Found favourite donut = $d")case _ => None }

輸出如下:

Step 5: How to pattern match on enumeration values Found favourite donut = Glazed Found favourite donut = Strawberry

最后,我們看下怎么改變Enum值的順序:

println("\nStep 6: How to change the default ordering of enumeration values") object DonutTaste extends Enumeration{type DonutTaste = Valueval Tasty = Value(0, "Tasty")val VeryTasty = Value(1, "Very Tasty")val Ok = Value(-1, "Ok") }println(s"Donut taste values = ${DonutTaste.values}") println(s"Donut taste of OK id = ${DonutTaste.Ok.id}")

輸出結果如下:

Step 6: How to change the default ordering of enumeration values Donut taste values = DonutTaste.ValueSet(Ok, Tasty, Very Tasty) Donut taste of OK id = -1

更多精彩內容且看:

  • 區塊鏈從入門到放棄系列教程-涵蓋密碼學,超級賬本,以太坊,Libra,比特幣等持續更新
  • Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續更新
  • Spring 5.X系列教程:滿足你對Spring5的一切想象-持續更新
  • java程序員從小工到專家成神之路(2020版)-持續更新中,附詳細文章教程

更多教程請參考 flydean的博客

總結

以上是生活随笔為你收集整理的Scala教程之:Enumeration的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。