Scala. Enumeration enumeration Example

Source: Internet
Author: User

Brief introduction

There is no enumeration type 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 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's 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
Object trafficlightcolor extends enumeration {  type trafficlightcolor  = value  val red = value (0,  "Stop")   val yellow =  value (  val green = value) ("Go")}object margin extends  enumeration {  type margin = value  val 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:  Trafficlightcolor)  = 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

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.