【发布时间】:2012-05-04 16:36:45
【问题描述】:
我看到一些代码写的特征如下:
trait SelfAware { self: Self =>
....
}
class Self
val s = new Self with SelfAware // this is ok
println(s.self) // error happened
class X
new X with SelfAware // error happened here
我想知道为什么会发生错误以及如何以这种方式使用 trait?
【问题讨论】:
-
我想知道为什么会发生错误以及如何以这种方式使用 trait?
-
我很高兴你遇到了这个错误,因为它我学到了更多关于特征的知识。 :) 尤其有趣的是,您可以通过这种方式指定 trait 可以应用于哪些类型的事物的限制。
-
因为这是我第一次使用这种类型的语法,我想知道
trait SomeTrait { this: SomeType => .... }和trait SomeTrait extends SomeType { ... }之间有什么区别?
标签: scala