Scala. Enumeration enumeration Example

Source: Internet
Author: User

Brief introduction

There are no enumeration types in Scala, but the enumeration class is provided in the standard class library to produce enumerations. After extending the enumeration class, call the value method class to initialize the possible values in the enumeration.

The inner class value is actually an abstract class, and the real creation is Val. Because it is actually Val, you can pass in the ID and name for value. If not specified, the ID is added to the previous enumeration value ID, and name is the field name.

Scala enumeration Example

Scala enumeration Example Object Trafficlightcolor extends enumeration {  type Trafficlightcolor = value  val Red = value (0, "Stop")  val Yellow = value (Ten)  val Green = value ("Go")}object margin extends enumeration {  type margin = value  VA L TOP, BOTTOM, left, right = Value}

 

Import Test. Trafficlightcolor._import test. Margin._object Driver extends App {  println (BOTTOM, bottom.id)  def dowhat (color:trafficlightcolor) = {    if ( color = = Red) "Stop"    else if (color = = Yellow) "Hurry up" Else "Go"  }  //Use match match  def DOWHAT2 (color:traff Iclightcolor) = Color match {case    Red    = ' Stop ' case    Yellow = ' Hurry up ' case    _      = ' Go ' 
   }  //Load red  val red = trafficlightcolor (0)//Calls enumeration.apply   println (Red, red.id)  println (Dowhat (red))  println (DOWHAT2 (trafficlightcolor.yellow))     //Print out all enumerations  Margin.values.foreach {v = println (v,v.id)}}

  

 

Scala. Enumeration enumeration example (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.