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
分享到:
相关推荐
R and data mining examples and case studies 对应的代码和数据
JSP Examples and Best Practices
Derived from years of experience working with data access developers, ADO.NET Examples and Best Practices for C# Programmers includes a set of techniques proven to drastically reduce overhead, ...
Java EE 7 tutorial 中的examples代码,找了很久才找到下来的,跟Java EE 7 tutorial配套使用,是官方的学习资料。
Companion Software for Digitale Signalverarbeitung: Grundlagen und Anwendungen Beispiele und Ubungen mit MATLAB (Digital Signal Processing: Fundamentals and ... Examples and Exercises with MATLAB)
ajax SimpleExamples
Probability: Theory and Examples,by Durrent,概率论专业经典教材。答案很少见,特意贡献~~~ 教材电子版已更新至第四版,可以在Durrent‘s homepage下载~!
Beginning Scala 2nd Edition takes a down to earth approach to teaching Scala that leads you through simple examples that can be combined to build complex scalable systems and applications This book ...
scala 编程思想 书籍中的源码,免费下载供大家使用
The core topics include the untyped lambda-calculus, simple type systems, type reconstruction, universal and existential polymorphism, subtyping, bounded quantification, recursive types, kinds, and ...
Filled with practical examples and use cases, this book will hot only help you get up and running with Spark, but will also take you farther down the road to becoming a data scientist.
2019 review and taxonomy on adversarial examples and defenses
This is a growing tutorial demonstrates some MATLAB examples and their equivalent code in Python
概率论书籍 Probability: Theory and Examples Rick Durrett
S7 Array and Indirect Addressing Examples and Basics.zip西门子PLC编程S7 Array and Indirect Addressing Examples and Basics.zip西门子PLC编程S7 Array and Indirect Addressing Examples and Basics.zip西门子...
programming-scala-1st-edition-code-examples programming-scala-1st-edition-code-examples programming-scala-1st-edition-code-examples programming-scala-1st-edition-code-examples
Probability - Theory and Examples
algorithm design foundations analysis and internet examples 中文版
S7 Array and Indirect Addressing Examples and Basics