scala中sealed关键字使用指南
1. 防止继承滥用
sealed关键字可以修饰类和特质(特质)。密封类提供了一种约束:不能在类定义的文件之外定义任何新的子类。然而,这样做有什么用呢?
在scala源码中List的实现用到了sealed关键字。
抽象类List有sealed关键字修饰,接下来Nil和::分别继承了该List抽象类。那我们来尝试实现基于List实现一个自己的List:
scala> class NewList extends List
<console>:7: error: illegal inheritance from sealed class Listclass NewList extends List
编译出错了,我们是不能在外部文件继承List类。所以,如果子类都明确的情况下,为了防止继承滥用,为抽象类添加sealed。
2. 模式匹配
模式匹配是scala非常好用的一个语法特性。但是,如果当条件经常改变,我们可能会忘了修改相应的case,那么这种情况就可能会出现错误。看下边例子:
scala> :pas
// Entering paste mode (ctrl-D to finish)abstract class Peoplecase object American extends Peoplecase object Japanese extends Peoplecase object Chinese extends Peoplecase object Russia extends Peopledef 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)Unitscala> people(American)
American personscala> 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: Russiadef 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到,减少编程的错误。
【完】
scala中sealed关键字使用指南相关推荐
- c#中sealed关键字的使用
c#中sealed关键字 前几天给大家总结了virtual(虚拟的).abstract(抽象的).override(重写)关键字,今天就抽空把sealed(密封的)给大家总结出来,因为这四个是一连串的 ...
- Scala中lazy关键字的使用和理解
Scala中lazy关键字的使用和理解 转载声明: 本文转自 Scala中lazy关键字的使用和理解 作者:br0x 转载仅为方便学习查看,一切权利属于原作者,如果带来不便请联系我删除. Scala中 ...
- 【scala】Scala中lazy关键字的使用和理解
Scala中使用关键字lazy来定义惰性变量,实现延迟加载(懒加载). 惰性变量只能是不可变变量,并且只有在调用惰性变量时,才会去实例化这个变量. 在Java中,要实现延迟加载(懒加载),需要自己手动 ...
- scala中def_def关键字以及Scala中的示例
scala中def Scala def关键字 (Scala def keyword) The def keyword in Scala is used to declare functions and ...
- Scala的sealed关键字
今天在看同事的代码时看到如下代码: package object msg { sealed trait TaskType { def toString: String } trait ...
- 快速了解sealed关键字
sealed关键字 sealed的英文意思就是密封,禁止的意思.在c#中sealed关键字可以用来修饰类和方法.作用于类时不能和abstract对象一起使用,因为使用了sealed修饰类表示此类不能被 ...
- scala中的sealed
sealed关键字在scala的集合和模式匹配上面应用较多,本篇文章说明sealed的使用场景以及为什么使用sealed. A sealed class cannot have any new sub ...
- scala语言示例_var关键字与Scala中的示例
scala语言示例 Scala var关键字 (Scala var keyword) The var Keyword in scala is used to declare variables. As ...
- c#中的sealed关键字
sealed关键字: 官方描述:应用于某个类时,sealed 修饰符可阻止其他类继承自该类. 还可以对替代基类中的虚方法或属性的方法或属性使用 sealed 修饰符. 这使你可以允许类派生自你的类并防 ...
最新文章
- iOS 在每一个cell上添加一个定时器的方案
- centos mysql数据迁移_Mysql 5.7.17 离线版安装和数据迁移(centos 7)
- 看大神如何使用vue导出pdf
- 格瑞光电:智慧城市建设的引领者
- nmap结果导出html,nmap的简单使用 - osc_ijgldkz9的个人空间 - OSCHINA - 中文开源技术交流社区...
- 3.js模式-策略模式
- 实现阿里云容器镜像服务反向访问代理
- vscode运行python文件_vscode怎么运行python文件
- 华为云大咖说-庄表伟:架构师的基本功——管理篇
- Null pointer access: The variable number can only be null at this location。 错误解决
- 河南大学计算机网络考试试卷,计算机网络试卷.doc
- 为什么蓝牙一主多从能解决集中式水表抄表难题?
- 12306 验证码验证流程
- Unity 3D中实现敌人追踪
- SpringBoot专栏:集成定时ScheduledTasks任务,刷新概念了_14讲
- 当下的力量(解读版)
- 使用Hourglass网络来理解人体姿态
- Unite之夜|千人派对开启狂欢周末,多重惊喜为你呈现!
- matlab一维图像程序,求大神指教啊!基于图像处理的一维条码识别(MATLAB程序)...
- Unity 相机 2D视角 与3D 视角 自由动态 切换