scala中sealed关键字使用指南
2016-03-10 17:22
387 查看
1. 防止继承滥用
sealed关键字可以修饰类和特质(特质)。密封类提供了一种约束:不能在类定义的文件之外定义任何新的子类。然而,这样做有什么用呢?在scala源码中List的实现用到了sealed关键字。
抽象类List有sealed关键字修饰,接下来Nil和::分别继承了该List抽象类。那我们来尝试实现基于List实现一个自己的List:
scala> class NewList extends List <console>:7: error: illegal inheritance from sealed class List class NewList extends List
编译出错了,我们是不能在外部文件继承List类。所以,如果子类都明确的情况下,为了防止继承滥用,为抽象类添加sealed。
2. 模式匹配
模式匹配是scala非常好用的一个语法特性。但是,如果当条件经常改变,我们可能会忘了修改相应的case,那么这种情况就可能会出现错误。看下边例子:scala> :pas // Entering paste mode (ctrl-D to finish) abstract class People case object American extends People case object Japanese extends People case object Chinese extends People case object Russia extends People def people(p: People) = p match { case American ⇒ println("American person") case Japanese ⇒ println("Japanese person") case Chinese ⇒ println("Chinese person") } // Exiting paste mode, now interpreting. defined class People defined object American defined object Japanese defined object Chinese defined object Russia people: (p: People)Unit scala> people(American) American person scala> people(Russia) scala.MatchError: Russia (of class Russia$) at .people(<console>:13) ... 33 elided
这时我们为People加上sealed,当我们编译代码时得到了一个警告:
<console>:18: warning: match may not be exhaustive. It would fail on the following input: Russia def people(p: People) = p match {
多温馨的一个提示啊。事情都不是绝对的,如果确定people只处理美国,日本,中国人,编译时总是给这么个警告也挺闹心。能破吗?可定能,看代码:
def people(p: People) = (p: @unchecked) match { case American ⇒ println("American person") case Japanese ⇒ println("Japanese person") case Chinese ⇒ println("Chinese person") }
3. sealed总结
从上面的描述我们可以知道,sealed 关键字主要有2个作用:其修饰的trait,class只能在当前文件里面被继承;
在检查模式匹配的时候,用sealed修饰目的是让scala知道这些case的所有情况,scala就能够在编译的时候进行检查,看你写的代码是否有没有漏掉什么没case到,减少编程的错误。
【完】
相关文章推荐
- Android Native 绘图方法
- Windows下Scala环境搭建
- PostgreSQL教程(三):表的继承和分区表详解
- C#中struct和class的区别详解
- Lua面向对象之类和继承浅析
- 浅析Ruby中继承和消息的相关知识
- VBS ArrayList Class vbs中的数组类
- 大家看了就明白了css样式中类class与标识id选择符的区别小结
- 设计引导--一个鸭子游戏引发的设计理念(多态,继承,抽象,接口,策略者模式)
- C++实现不能被继承的类实例分析
- PHP类的封装与继承详解
- 深入了解PHP类Class的概念
- js继承 Base类的源码解析
- jquery 表单验证之通过 class验证表单不为空
- Javascript 面向对象 继承
- JavaScript 继承使用分析
- Javascript面向对象编程(二) 构造函数的继承
- javascript面向对象之Javascript 继承
- JavaScript 对象、函数和继承
- 学习javascript面向对象 javascript实现继承的方式