密封类
代码
/*
密封类
1.可以避免滥用继承
2.用在模式匹配
*/
sealed abstract class Furniture
case class Couch() extends Furniture
case class Chair() extends Furniture
class SealedDemo {
def findPlaceTosit(furniture: Furniture):String=furniture match{
case a:Couch=>"lie on the couch"
case b:Chair=>"site on the chair"
}
}
object test{
def main(args: Array[String]): Unit = {
val chair=Chair()
val sea=new SealedDemo()
println(sea.findPlaceTosit(chair))
}
}
运行结果
site on the chair
Process finished with exit code 0