learning scala pattern matching 03

 code: 

package com.aura.scala.day01

object patternMatching03 {
  //当不同类型对象需要调用不同方法时,仅匹配类型的模式非常有用。
  def goIDLE(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"
}

 

posted @ 2019-07-10 13:13  嵌入式实操  阅读(112)  评论(0编辑  收藏  举报