黑猴子的家:Scala 密封类

如果想让case类的所有子类都必须在申明的该类的相同的文件中定义,可以将样例类的通用超类声明为sealed,叫做密封类,密封就是外部用户不能在其他文件中定义子类。不能被继承只能自己用

sealed abstract class TrafficLightColor
case object Red extends TrafficLightColor
case object Yellow extends TrafficLightColor
case object Green extends TrafficLightColor

尖叫提示:密封类,只能在同一个文件中定义子类

转载于:https://www.jianshu.com/p/ffce6c809e64

猜你喜欢

转载自blog.csdn.net/weixin_33860553/article/details/91182462