在阅读有关Scala的一些文章时,我发现了一些带有好奇语法的例子,我可能会错误地理解它
class Child[C <: Child[C]] {
some_name : C => // here, what does it mean?
var roomie : Option[C] = None
def roomWith(aChild : C)= {
roomie = Some(aChild)
aChild.roomie = Some(this)
}
}
class Boy extends Child[Boy]
Run Code Online (Sandbox Code Playgroud)
我找到了类似特征的例子.
这是否意味着我this在类范围内声明对象的类型C?
Jai*_*rge 10
它是一种自我类型的注释.
这意味着类Child必须是类型C,即创建必须满足给定类的继承依赖项.
一个小例子:
scala> trait Baz
defined trait Baz
scala> class Foo {
| self:Baz =>
| }
defined class Foo
scala> val a = new Foo
<console>:9: error: class Foo cannot be instantiated because it does not conform to its self-type Foo with Baz
val a = new Foo
^
scala> val a = new Foo with Baz
a: Foo with Baz = $anon$1@199de181
scala> class Bar extends Foo with Baz
defined class Bar
Run Code Online (Sandbox Code Playgroud)
在这种情况下Foo也需要是一个Baz.满足该要求,Foo可以创建一个实例.此外,定义一个新类(在这种情况下Bar)也有它的要求Baz.
见:http: //www.scala-lang.org/node/124
| 归档时间: |
|
| 查看次数: |
312 次 |
| 最近记录: |