`
sillycat
  • 浏览: 2539903 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

scala(13)A Tour of Scala: Explicitly Typed Self References

 
阅读更多
scala(13)A Tour of Scala: Explicitly Typed Self References

Explicitly Typed Self References
…snip…

Subclassing
class Point(xc: Int, yc: Int){
     val x: Int = xc
     val y: Int = yc
     def move(dx: Int, dy: Int): Point =
          new Point(x+dx, y+ dy)
}

class ColorPoint(u: Int, v: Int, c: String) extends Point(u, v) {
     val color: String = c
     def compareWith(pt: ColorPoint) : Boolean =
          (px.x == x) && (pt.y == y) && (pt.color == color)
     override def move(dx: Int, dy: Int) : ColorPoint =
          new ColorPoint(x + dy, y + dy, color)
}

Local Type Inference
Deduce the type from the initialization expression of the variable and return response of the return type.
  def main(args: Array[String]): Unit = {
    valx = 1 + 2 * 3// the type is Int
    def succ(x: Int) :Int = x+ 1// the return type should be :Int and we can ommit it
   

  }

And remember the recursive methods can not deduce the type of return.

Unified Types
All the values are objects in scala.

Variance
package com.sillycat.easyscala.start.tour.tour4

object VariancesTest extends App {
  vars: Stack[Any] = new Stack().push("hello");
  s = s.push(new Object())
  s = s.push(7)
  Console.println(s) //7 java.lang.Object@5b6df84b hello
}

class Stack[+A] {
  def push[B >: A](elem: B): Stack[B] = new Stack[B] {
    overridedef top: B = elem
    overridedef pop: Stack[B] = Stack.this
    overridedef toString() = elem.toString() + " " +
      Stack.this.toString()
  }
  def top: A = sys.error("no element on stack")
  def pop: Stack[A] = sys.error("no element on stack")
  overridedef toString() = ""

}

Views
…snip…

XML Processing
…snip...

References:
http://www.scala-lang.org/node/104
http://www.scala-lang.org/node/124
http://www.scala-lang.org/node/125
http://www.scala-lang.org/node/127
http://www.scala-lang.org/node/128
http://www.scala-lang.org/node/130
http://www.scala-lang.org/node/131

scala(12) http://sillycat.iteye.com/blog/1842158
分享到:
评论

相关推荐

    scala-parser-combinators_2.11-1.0.4-API文档-中英对照版.zip

    赠送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-parser-combinators_2.12-1.1.0-API文档-中英对照版.zip

    赠送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-parser-combinators-2.11-1.0.4-API文档-中文版.zip

    赠送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依赖信息...

    Scala函数式编程

    很大篇幅都放在,使用scala实现scala默认库文件的API中,通过对简单的函数式编程逻辑的介绍和实践,主要是实践,建立起来一个比较明晰的scala思维模式,或者叫函数式编程的思维模式。 2 无副作用的函数式编程,同时...

    2018 Scala for Java Developers: A Practical Primer

    Learn Scala is split into four parts: a tour of Scala, a comparison between Java and Scala, Scala-specific features and functional programming idioms, and finally a discussion about adopting Scala in...

    scala-compiler-2.11.12-API文档-中文版.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; 包含...

    scala-compiler-2.11.8-API文档-中英对照版.zip

    赠送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-compiler-2.12.7-API文档-中文版.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; 包含翻译后...

    scala-compiler-2.11.0-API文档-中文版.zip

    赠送jar包:scala-compiler-2.11.0.jar; 赠送原API文档:scala-compiler-2.11.0-javadoc.jar; 赠送源代码:scala-compiler-2.11.0-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.0.pom; 包含翻译后...

    scala-reflect-2.12.10-API文档-中文版.zip

    赠送jar包:scala-reflect-2.12.10.jar; 赠送原API文档:scala-reflect-2.12.10-javadoc.jar; 赠送源代码:scala-reflect-2.12.10-sources.jar; 赠送Maven依赖信息文件:scala-reflect-2.12.10.pom; 包含翻译后...

    scala-reflect-2.12.7-API文档-中文版.zip

    赠送jar包:scala-reflect-2.12.7.jar; 赠送原API文档:scala-reflect-2.12.7-javadoc.jar; 赠送源代码:scala-reflect-2.12.7-sources.jar; 赠送Maven依赖信息文件:scala-reflect-2.12.7.pom; 包含翻译后的API...

    scala-reflect-2.11.8-API文档-中文版.zip

    赠送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...

    scala-reflect-2.11.12-API文档-中英对照版.zip

    赠送jar包:scala-reflect-2.11.12.jar; 赠送原API文档:scala-reflect-2.11.12-javadoc.jar; 赠送源代码:scala-reflect-2.11.12-sources.jar; 赠送Maven依赖信息文件:scala-reflect-2.11.12.pom; 包含翻译后...

    scala-library-2.11.8-API文档-中文版.zip

    赠送jar包:scala-library-2.11.8.jar; 赠送原API文档:scala-library-2.11.8-javadoc.jar; 赠送源代码:scala-library-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-library-2.11.8.pom; 包含翻译后的API...

    scala-compiler-2.11.0-API文档-中英对照版.zip

    赠送jar包:scala-compiler-2.11.0.jar; 赠送原API文档:scala-compiler-2.11.0-javadoc.jar; 赠送源代码:scala-compiler-2.11.0-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.0.pom; 包含翻译后...

    scala-compiler-2.12.7-API文档-中英对照版.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; 包含翻译后...

    scala-xml_2.11-1.0.1-API文档-中文版.zip

    赠送jar包:scala-xml_2.11-1.0.1.jar; 赠送原API文档:scala-xml_2.11-1.0.1-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.1-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.1.pom; 包含翻译后的API...

    scala-reflect-2.11.8-API文档-中英对照版.zip

    赠送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...

    scala-xml_2.11-1.0.4-API文档-中英对照版.zip

    赠送jar包:scala-xml_2.11-1.0.4.jar; 赠送原API文档:scala-xml_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.4-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.4.pom; 包含翻译后的API...

    scala-2.12.12 windows版本安装包

    有需要下载 scala-2.12.12.msi 的小伙伴,可以直接下载,官网下载实在是太慢了,o(╥﹏╥)o

Global site tag (gtag.js) - Google Analytics