self type 作用通常为了分割大类到多个特质traits的。比如cake pattern蛋糕模式中会用到self type
self type
A self type of a trait is the assumed type of this
, the receiver, to be used within the trait. Any concrete class that mixes in the trait must ensure that its type conforms to the trait’s self type. The most common use of self types is for dividing a large class into several traits (as described in Chapter 29 of Programming in Scala.
例子在<<Programming in Scala>> 第29章 第4小节给出的例子代码如下
package quickanswer.chapter10class Food(fn:String){var name:String=fn}class Recipe(var rn:String,var foodLst:List[Food],var desc:String){//食谱}trait FoodCategories {case class FoodCategory(name: String, foods: List[Food])def allCategories: List[FoodCategory]}trait SimpleFoods {object Pear extends Food("Pear")object Apple extends Food("Apple")def allFoods = List(Apple, Pear)def allCategories = Nil}abstract class Database extends FoodCategories {def allFoods: List[Food]def allRecipes: List[Recipe]def foodNamed(name: String) =allFoods.find(f => f.name == name)}object SimpleDatabase extends Database with SimpleFoods with SimpleRecipestrait SimpleRecipes { // Does not compile// this: SimpleFoods => //注释掉这句话会编译错误,因为Apple, Pear 是定义在SimpleFoods 的object FruitSalad extends Recipe("fruit salad",List(Apple, Pear), // Uh oh"Mix it all together.")def allRecipes = List(FruitSalad)}object SplittingModulesIntoTraits extends App {}
dividing a large class( SimpleDatabase ) into several traits (SimpleFoods ,SimpleRecipes),而在 定义trait SimpleRecipes 中又需要SimpleFoods 中的内容, 但是编译器器并不知道 SimpleRecipes 是混入SimpleFoods 的,而 Apple, Pear 是定义在SimpleFoods中的的,所以编译会报错 , 而通过self type 解决了这个问题
当SimpleRecipes 添加 this: SimpleFoods => 后,Pear会被暗中转化为this.Pear ,这样是安全的,因为任何混入SimpleRecipes 的具体类必须也要混入SimpleFoods 。这意味着Pear会是这个具体类的一个成员。混入SimpleRecipes 的抽象类和trait没哟这个限制,但是他们是不能通过new 实例化的。所哟this.Pear 是安全的
书本解释如下"
trait SimpleRecipes {this: SimpleFoods =>object FruitSalad extends Recipe("fruit salad",List(Apple, Pear), // Now Pear is in scope"Mix it all together.")def allRecipes = List(FruitSalad)}
相关推荐
赠送jar包:scala-java8-compat_2.11-0.7.0.jar; 赠送原API文档:scala-java8-compat_2.11-0.7.0-javadoc.jar; 赠送源代码:scala-java8-compat_2.11-0.7.0-sources.jar; 赠送Maven依赖信息文件:scala-java8-...
赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 赠送Maven依赖信息...
赠送jar包:scala-java8-compat_2.11-0.7.0.jar; 赠送原API文档:scala-java8-compat_2.11-0.7.0-javadoc.jar; 赠送源代码:scala-java8-compat_2.11-0.7.0-sources.jar; 赠送Maven依赖信息文件:scala-java8-...
IntellJIDEA Scala插件:scala-intellij-bin-2016.3.9
赠送jar包:scala-compiler-2.11.8.jar; 赠送原API文档:scala-compiler-2.11.8-javadoc.jar; 赠送源代码:scala-compiler-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.8.pom; 包含翻译后...
scala eclipse插件.对应scala版本:2.10--2.11,对应eclipes版本:4.4--4.5. update site:http://download.scala-ide.org/sdk/lithium/e44/scala211/stable/site 下载地址:...
scala-SDK-4.7.0-vfinal-2.12-win32.win32.x86_64 scala-SDK-4.7.0-vfinal-2.12-win32.win32.x86_64
scala-SDK-4.7.0-vfinal-2.12-linux.gtk.x86_64.tar.gz scala-SDK-4.7.0-vfinal-2.12-linux.gtk.x86_64.tar.gz
赠送jar包:scala-parser-combinators_2.12-1.1.0.jar; 赠送原API文档:scala-parser-combinators_2.12-1.1.0-javadoc.jar; 赠送源代码:scala-parser-combinators_2.12-1.1.0-sources.jar; 赠送Maven依赖信息...
scala-SDK-3.0.1-vfinal-2.10-win32.win32.x86.part01.rar
scala-intellij-bin-2021.3.6.zip
赠送jar包:scala-compiler-2.11.12.jar; 赠送原API文档:scala-compiler-2.11.12-javadoc.jar; 赠送源代码:scala-compiler-2.11.12-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.12.pom; 包含...
赠送jar包:scala-reflect-2.11.8.jar; 赠送原API文档:scala-reflect-2.11.8-javadoc.jar; 赠送源代码:scala-reflect-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-reflect-2.11.8.pom; 包含翻译后的API...
IntelliJ IDEA 官网下载 scala 插件,为大家提速 scala-intellij-bin-2016.3.1.zip
scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!
IDEA 全称 IntelliJ IDEA,是java编程语言开发的集成环境。IntelliJ在业界被公认为最好的java开发工具,尤其在智能代码助手、代码自动提示、重构、JavaEE支持、各类版本工具(git、...通过该插件可支持Scala语言的构建。
赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 包含翻译后的API...
scala-intellij-bin-2020.2.3.zip
scala-intellij-bin-2020.3.14.zip
赠送jar包:scala-compiler-2.12.7.jar; 赠送原API文档:scala-compiler-2.12.7-javadoc.jar; 赠送源代码:scala-compiler-2.12.7-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.12.7.pom; 包含翻译后...