ScalaTutorial(1)Simple Examples and Types

本文介绍Scala编程语言的基本概念,包括简单的例子、类型系统、对象、类、继承、匿名函数、方法和类参数等内容。通过具体示例展示了Scala如何实现面向对象编程、函数式编程和操作类与对象的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值