learning scala pattern matching 03

 code: 

Package Comkawrksclkday0l

Object patternMatching03 {
   // when different types of objects need to call different methods, only the type of the matching pattern is useful. 
  goIdle DEF (Device: Device) =   Device match {
     Case P: Phone => p.screenOff
     Case C: Computer => c.screenSaverOn
  }
}
abstract class Device

case class Phone(model:String) extends  Device{
  def screenOff = "Turning screen off"
}

 

Guess you like

Origin www.cnblogs.com/lianghong881018/p/11163274.html