Si PartialFunction est un trait, alors comment fonctionne ce code? Créons-nous un objet du trait?Comment créons-nous un objet pour un 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")
Vous créez une implémentation anonyme du trait - tout comme vous créez une implémentation anonyme d'une interface en Java. –