A Scala Tutorial for Java programmers
2016-12-07 09:34
453 查看
Scala
Scalahello world
Interaction with Java
Object Numbers are objects
Object Functions are objects
Object Anonymous functions
Classes Methods without arguments
Classes Fields
Classes Inheritance and overriding scalaAnyRef
Case classes and pattern matching
Traits
Genericity
hello world
$ cat HelloWorld.scala
object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } }
$ scalac HelloWorld.scala && scala -classpath . HelloWorld Hello, world!
Interaction with Java
$ cat ChinaDate.scala
import java.util.{Date, Locale} import java.text.DateFormat import java.text.DateFormat._ object ChinaDate { def main(args: Array[String]) { val now = new Date val df = getDateInstance(LONG, Locale.CHINA) println(df format now) } }
$ scalac ChinaDate.scala && scala -classpath . ChinaDate 2016年12月7日 $ sed -i "s/df format now/df.format(now)/" ChinaDate.scala $ scalac ChinaDate.scala && scala -classpath . ChinaDate 2016年12月7日
Object: Numbers are objects
scala> val x = 5 x: Int = 5 scala> 1 + 2 * 3 / x res1: Int = 2 scala> (1).+(((2).*(3))./(x)) res2: Int = 2 scala> 1.+(2) res3: Int = 3 scala> (1).+(2) res4: Int = 3
Object: Functions are objects
$ cat Timer.scala
object Timer { def oncePerSecond(callback: () => Unit) { while (true) { callback(); Thread sleep 1000 } } def timeFlies() { println("time flies like an arrow...") } def main(args: Array[String]) { oncePerSecond(timeFlies) } }
$ scalac Timer.scala && scala -classpath . Timer warning: there was one deprecation warning (since 2.12.0); re-run with -deprecation for details one warning found time flies like an arrow... time flies like an arrow... time flies like an arrow... ^C
Object: Anonymous functions
$ cat TimerAnonymous.scala
object TimerAnonymous { def oncePerSecond(callback: () => Unit) { while (true) { callback(); Thread sleep 1000 } } def main(args: Array[String]) { oncePerSecond(() => println("time flies like an arrow...")) } }
$ scalac TimerAnonymous.scala && scala -classpath . TimerAnonymous time flies like an arrow... time flies like an arrow... time flies like an arrow... ^C
Classes: Methods without arguments
$ cat ComplexNumbers.scala
class Complex(real: Double, imaginary: Double) { def re() = real def im() = imaginary } object ComplexNumbers { def main(args: Array[String]) { val c = new Complex(1.2, 3.4) println("imaginary part: " + c.im()) } }
$ scalac ComplexNumbers.scala && scala -classpath . ComplexNumbers imaginary part: 3.4
Classes: Fields
$ cat ComplexNumbers.scala
class Complex(real: Double, imaginary: Double) { def re = real def im = imaginary } object ComplexNumbers { def main(args: Array[String]) { val c = new Complex(1.2, 3.4) println("real part: " + c.re) } }
$ scalac ComplexNumbers.scala && scala -classpath . ComplexNumbers real part: 1.2
Classes: Inheritance and overriding (scala.AnyRef)
$ cat ComplexNumbers.scala
class Complex(real: Double, imaginary: Double) { def re = real def im = imaginary override def toString() = "" + re + (if (im < 0) "" else "+") + im + "i" } object ComplexNumbers { def main(args: Array[String]) { val c = new Complex(1.2, 3.4) println("complex: " + c) } }
$ scalac ComplexNumbers.scala && scala -classpath . ComplexNumbers complex: 1.2+3.4i
Case classes and pattern matching
$ cat Fomulate.scala
abstract class Tree case class Sum(l: Tree, r: Tree) extends Tree case class Var(n: String) extends Tree case class Const(v: Int) extends Tree object Fomulate { type Environment = String => Int def eval(t: Tree, env: Environment): Int = t match { case Sum(l, r) => eval(l, env) + eval(r, env) case Var(n) => env(n) case Const(v) => v } def derive(t: Tree, v: String): Tree = t match { case Sum(l, r) => Sum(derive(l, v), derive(r, v)) case Var(n) if (v == n) => Const(1) case _ => Const(0) } def main(args: Array[String]) { val exp: Tree = Sum(Sum(Var("x"),Var("x")),Sum(Const(7),Var("y"))) val env: Environment = { case "x" => 5 case "y" => 7 } println("Expression: " + exp) println("Evaluation with x=5, y=7: " + eval(exp, env)) println("Derivative relative to x:\n " + derive(exp, "x")) println("Derivative relative to y:\n " + derive(exp, "y")) } }
$ scalac Fomulate.scala && scala -classpath . Fomulate Expression: Sum(Sum(Var(x),Var(x)),Sum(Const(7),Var(y))) Evaluation with x=5, y=7: 24 Derivative relative to x: Sum(Sum(Const(1),Const(1)),Sum(Const(0),Const(0))) Derivative relative to y: Sum(Sum(Const(0),Const(0)),Sum(Const(0),Const(1)))
Traits
$ cat Date.scala
trait Ord { def < (that: Any): Boolean def <=(that: Any): Boolean = (this < that) || (this == that) def > (that: Any): Boolean = !(this <= that) def >=(that: Any): Boolean = !(this < that) } class Date(y: Int, m: Int, d: Int) extends Ord { def year = y def month = m def day = d override def toString(): String = year + "-" + month + "-" + day override def equals(that: Any): Boolean = that.isInstanceOf[Date] && { val o = that.asInstanceOf[Date] o.day == day && o.month == month && o.year == year } def <(that: Any): Boolean = { if (!that.isInstanceOf[Date]) sys.error("cannot compare " + that + " and a Date") val o = that.asInstanceOf[Date] (year < o.year) || (year == o.year && (month < o.month || (month == o.month && day < o.day))) } }
$ scalac Date.scala
Genericity
$ cat IntegerReference.scala
class Reference[T] { private var contents: T = _ def set(value: T) { contents = value } def get: T = contents } object IntegerReference { def main(args: Array[String]) { val cell = new Reference[Int] cell.set(13) println("Reference contains the half of " + (cell.get * 2)) } }
$ scalac IntegerReference.scala && scala -classpath . IntegerReference Reference contains the half of 26
相关文章推荐
- A Scala Tutorial for Java programmers之(一)Scala入门:Scala例子,以及如何与Java交互
- A Scala Tutorial for Java Programmers
- UML For Java Programmers(1)
- Cairo Tutorial for Python Programmers
- TCP/IP Sockets in Java, Second Edition: Practical Guide for Programmers
- 【GO for java programmers】面向Java开发者的GO编程2
- 【GO for java programmers】面向Java开发者的GO编程1
- 鲍勃大叔的组合拳——清华社[UML for Java Programmers]
- 10 Hibernate Interview Questions and Answers for Java J2EE Programmers
- Computer Graphics for Java Programmers
- Foundations of Java for ABAP Programmers
- 【GO for java programmers】面向Java开发者的GO编程4
- 关于《Cairo Tutorial for Python Programmers 》
- UML for Java Programmers下载
- .NET Framework For Java Programmers ---3(Good)
- Python Programming with the Java(TM) Class Libraries: A Tutorial for Building Web and Enterprise App
- UML for Java Programmers中文共享版
- .NET Framework For Java Programmers ---1(Good)
- UML for Java Programmers
- 《UML for Java Programmers》记