3 回答
TA贡献1776条经验 获得超12个赞
B extends A
B
是A
B
要求A
如果 B
是扩展的,那么你. 所需
混入 A
.当一个具体的类最终扩展/混合-在这些特征中,一些类/特征必须实现 A
.
scala> trait User { def name: String }defined trait Userscala> trait Tweeter { | user: User => | def tweet(msg: String) = println(s"$name: $msg") | }defined trait Tweeterscala> trait Wrong extends Tweeter { | def noCanDo = name | }<console>:9: error: illegal inheritance; self-type Wrong does not conform to Tweeter's selftype Tweeter with User trait Wrong extends Tweeter { ^<console>:10: error: not found: value name def noCanDo = name ^
Tweeter
User
User
Tweeter
User
Wrong
scala> trait DummyUser extends User { | override def name: String = "foo" | }defined trait DummyUserscala> trait Right extends Tweeter with User { | val canDo = name | }defined trait Right scala> trait RightAgain extends Tweeter with DummyUser { | val canDo = name | }defined trait RightAgain
Right
User
User
Right
.
RightAgain
User
User
TA贡献1831条经验 获得超4个赞
trait A { self: B => }trait B { self: A => }
extends
trait A extends Btrait B extends A error: illegal cyclic reference involving trait A
在电子表格示例中,类模型继承了计算器,从而获得了对其计算方法的访问权。另一方面,类评估器将其Self类型定义为模型,如下所示:
package org.stairwaybook.scellstrait Evaluator { this: Model => ...
TA贡献1898条经验 获得超8个赞
trait Foo{ this: { def close:Unit} => ...}
添加回答
举报