如果 PartialFunction 是一个特征,那么这段代码是如何工作的?我们是在创建一个 trait 的对象吗?
def x=new PartialFunction[Any, Unit] {
def isDefinedAt(x: Any) = x match {
case "hello" => true
case "world" => true
case _=> false
}
def apply(x: Any) = x match {
case "hello" => println("Message received hello")
case "world"=> println("Message received world")
}
}
x("hello")
if (x.isDefinedAt("bye") ){ x("bye")}
x("bye")