加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala中抽象特征的语义学

发布时间:2020-12-16 09:33:35 所属栏目:安全 来源:网络整理
导读:我想知道使用抽象关键字与特征结合使用的语义是什么。 如果trait没有定义任何抽象方法,那么abstract关键字不会阻止我创建一个实例: scala abstract trait Tdefined trait Tscala new T{}res0: java.lang.Object with T = $anon$1@12cd927d 另一方面,如果t
我想知道使用抽象关键字与特征结合使用的语义是什么。

如果trait没有定义任何抽象方法,那么abstract关键字不会阻止我创建一个实例:

scala> abstract trait T
defined trait T

scala> new T{}
res0: java.lang.Object with T = $anon$1@12cd927d

另一方面,如果trait确实定义了一个抽象方法,我无法创建一个实例(当然没有实现这个方法),无论抽象关键字是否存在:

scala> abstract trait T { def foo : Unit }
defined trait T

scala> new T{}
<console>:9: error: object creation impossible,since method foo in trait T of type =>    Unit is not defined
              new T{}
                  ^

scala> trait T { def foo : Unit }
defined trait T

scala> new T{}
<console>:9: error: object creation impossible,since method foo in trait T of type =>     Unit is not defined
              new T{}
                  ^

那么抽象关键字在一个特征之前是什么呢?

解决方法

它没有效果,特征是自动抽象的。

The abstract modifier is used in class definitions. It is redundant
for traits,and mandatory for all other classes which have incomplete
members.

http://www.scala-lang.org/docu/files/ScalaReference.pdf

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读