前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[7] - trait

[7] - trait

作者头像
codingforfun
发布2018-08-24 15:10:13
3280
发布2018-08-24 15:10:13
举报

当你开始使用继承来重用代码时,你入门了;当你开始避免使用继承来重用代码时,你成熟了

这是我以前在知乎上看到关于类继承作用的回答,虽不完全正确,却十分明确的表达出了好的代码应避免类继承而尽量使用类组合。Scala 显然也非常赞同这一点,以至于有了 trait,又叫做特质。当我们定义特质时,应该要遵循这样的原则:一个 trait 只干一件事,如果要干多件事,就定义多个 trait,然后使用一个类来 extends 这些 traits

定义 trait

trait 的定义与 class 类似:

代码语言:javascript
复制
scala> trait T {
     | }
defined trait T

当然,trait 可以包含成员和方法,并且:

  • trait 中的成员可以仅声明,也可以声明并指定值
  • trait 中的方法可以有实现,也可以只有声明而没有实现
代码语言:javascript
复制
scala> trait T {
     |   val a: Int
     |   val b: Int = 1
     |
     |   def getA(): Int
     |   def getB() = b
     | }
defined trait T

对比而言,类一旦包含未定义的方法就必须声明为 abstract;而 Java 的接口中的方法是不能实现的,必须是抽象方法。如果 trait 既为实现它所声明的方法,也没有定义或声明其他成员,那么在字节码级别,该 trait 其实是接口是相同的

另一个与类不同的是,trait 主构造函数不允许有参数列表,并且不允许为 trait 定义辅助构造函数

混入多个 trait

Scala 类只能有一个父类,但可以混入多个 trait,当要混入多个 traits 或已经继承了某个父类时,需要使用关键字 with,如下例:

代码语言:javascript
复制
scala> trait T {
     |   val a: Int
     |   val b: Int = 1
     |
     |   def getA(): Int
     |   def getB() = b
     | }
defined trait T

scala>

scala> trait Q {
     |   def currentTime: String = System.currentTimeMillis().toString
     | }
defined trait Q

scala>

scala> class X extends T with Q {
     |   override val a = 1
     |   override def getA(): Int = a
     | }
defined class X

当类混入 trait 时,需要实现 trait 中为实现的成员和方法。要混入多个 trait 是为了保证『高内聚』,通俗说就是一个 trait 只干一件事,如果要干多件事,就定义多个 trait 然后混入它们


当你继承的父类和混入的特质或混入的不同特质之间有同名方法时可能会有冲突,分为以下几种情况:

  • trait 中的方法未实现:不会冲突
代码语言:javascript
复制
scala> class C {
     |   def a: String = "a"
     | }
defined class C

scala>

scala> trait T {
     |   def a: String
     | }
defined trait T

scala>

scala> trait Q extends C with T {}
defined trait Q
  • trait 中的方法实现了且与父类中的方法参数列表及返回类型相同:会冲突
代码语言:javascript
复制
scala> class C {
     |   def a: String = "a"
     | }
defined class C

scala>

scala> trait T {
     |   def a: String = ""
     | }
defined trait T

scala>

scala> trait Q extends C with T {}
<console>:9: error: trait Q inherits conflicting members:
  method a in class C of type => String  and
  method a in trait T of type => String
(Note: this can be resolved by declaring an override in trait Q.)
       trait Q extends C with T {}
             ^
  • trait 中的方法实现了且与父类中的参数列表相同,返回类型不同:会冲突
代码语言:javascript
复制
scala> class C {
     |   def a: String = "a"
     | }
defined class C

scala>

scala> trait T {
     |   def a: Int = 1
     | }
defined trait T

scala>

scala> trait Q extends C with T {}
<console>:9: error: trait Q inherits conflicting members:
  method a in class C of type => String  and
  method a in trait T of type => Int
(Note: this can be resolved by declaring an override in trait Q.)
       trait Q extends C with T {}
             ^
  • trait 中的方法实现了且与父类的参数列表不同,返回类型相同:不会冲突
代码语言:javascript
复制
scala> class C {
     |   def a: String = "a"
     | }
defined class C

scala>

scala> trait T {
     |   def a( i: Int ): String = i.toString
     | }
defined trait T

scala>

scala> trait Q extends C with T {}
defined trait Q

trait 的继承

一个 trait 同样可以混入其他 trait 或继承类:

代码语言:javascript
复制
scala> class C {
     |   def currentTime: String = System.currentTimeMillis().toString
     | }
defined class C

scala>

scala> trait T {
     |   def random: Int
     | }
defined trait T

scala>

scala> trait Q extends C with T {}
defined trait Q

虽然 Scala 语言支持你这么做,但我个人并不推荐


**传送门: **Scala 在简书目录


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.05.31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 定义 trait
  • 混入多个 trait
  • trait 的继承
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档