使用这个关键字来继承? [英] Using this keyword to inherit?

查看:72
本文介绍了使用这个关键字来继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
scala self-types 和特征子类?

来自 Scalatest 站点中的示例.有一件事我不太明白

From example in scalatest site. There is one particular thing I don't really understand

trait FunSuiteStackBehaviors { 
                   this: FunSuite => //This line
                         def a() {}
                         def b() {}
}

class StackFunSuite extends FunSuite with FunSuiteStackBehaviors {}

据我所知,他们似乎试图将一些定义分配给一个特征.但是 this: FunSuite => 部分做了什么?我尝试使用 extends FunSuite 而不是

As far as I understand, it seems like they try to assign some defs into a trait. But what does this: FunSuite => part do ? I tried to use extends FunSuite instead like

trait FunSuiteStackBehaviors extends FunSuite { 
                         def a() {}
                         def b() {}
}

class StackFunSuite extends FunSuite with FunSuiteStackBehaviors {}

我仍然得到相同的结果.它们是一样的吗?

and I still end up with same result. Are they the same thing ?

推荐答案

this: => XXXX 被称为自类型注解 http://www.scala-lang.org/node/124基本上,您将this"(当前对象)的类型指定为指定的类型.一种从内部投射"

the this: => XXXX is called a self type annotation http://www.scala-lang.org/node/124 Basically, you're specifying the type of "this" (current object) to the type specified. Kind of a "cast from the inside"

这篇关于使用这个关键字来继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆