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

ScalaTutorial(1)Simple Examples and Types

 
阅读更多
ScalaTutorial(1)Simple Examples and Types

1. Introduction
2. A first Example
2.1 Compiling the Example
scalac is Scala compiler.
>scalac HelloWorld.scala
This command will generate the HelloWorld.class file in that directory.

2.2. Running the example
>scala HelloWorld

3. Interaction with Java
All classes from the java.lang package are imported by default, while others need to be imported explicitly.
package com.sillycat.easyscala
import java.text.DateFormat
import java.util.Date
import java.util.Locale
object FrenchDate {
  def main(args: Array[String]): Unit = {
    val now = new Date
    val df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE)
    println(df format now)
  }
}

df format now is less verbose way of writing df.format(now).

It is possible to inherit from Java classes and implement Java interfaces directly in Scala.

4. Everything is an object
Everything is an object in Scala, including numbers or functions. Since Java distinguishes primitive types(such as boolean and int) from reference types.

4.1 Numbers are objects
print(1+2*3)

4.2 Functions are objects
Since functions are also objects in Scala, it is therefore possible to pass functions as arguments, to store them in variables, and to return them from other functions.

This ability to manipulate functions as values is one of the cornerstone of a very interesting programming paradigm called functional programming.
package com.sillycat.easyscala
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]): Unit = {
    oncePerSecond(timeFlies)
  }
}

4.2.1. Anonymous Functions
package com.sillycat.easyscala
object TimerAnonymous {
  def oncePerSecond(callback:() => Unit){
    while(true){callback();Thread sleep 1000}
  }
  def main(args: Array[String]): Unit = {
    oncePerSecond(()=>println("time goes"))
  }
}

"=>" this separates the function's argument list from its body.

5. Classes
Classes in Scala can have parameters.
package com.sillycat.easyscala

class Complex(real: Double, imaginary: Double) {
  def re() = real
  def im() = imaginary
}

5.1 Methods without arguments
package com.sillycat.easyscala
object ComplexNumbers {
  def main(args:Array[String]) {
    val c= new Complex(1.3,3.4)
    println("imaginary part:" + c.im())
  }
}

re and im are methods without arguments.

without (), this definetion will be ok:
  def re = real
  def im = imaginary
and   println("imaginary part:" + c.im)

5.2 Inheritance and overriding
All classes in Scala inherit from a super-class, scala.AnyRef.

We will use override modifier in order to avoid accidental overriding.
override def toString() = {
     "" + re + (if (im < 0) "" else "+") + im + "i"
  }

println(c)

references:

books:
ScalaTutorial.pdf

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics