`
sillycat
  • 浏览: 2542873 次
  • 性别: 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

分享到:
评论

相关推荐

    Dataset for R and data mining examples and case studies

    本数据集"Dataset for R and data mining examples and case studies"旨在提供一系列R语言编程实例和数据挖掘案例研究,帮助用户深入理解和掌握相关技术。 首先,让我们来探讨R语言在数据处理中的优势。R语言拥有...

    Digital Signal Processing: Fundamentals and Applications; Examples and Exercises with MATLAB

    Companion Software for Digitale Signalverarbeitung: Grundlagen und Anwendungen Beispiele und Ubungen mit MATLAB (Digital Signal Processing: Fundamentals and ... Examples and Exercises with MATLAB)

    Probability: Theory and Examples答案

    Probability: Theory and Examples,by Durrent,概率论专业经典教材。答案很少见,特意贡献~~~ 教材电子版已更新至第四版,可以在Durrent‘s homepage下载~!

    atomic-scala-examples

    《原子Scala示例》是基于Scala编程语言的一系列实践代码,源自知名编程书籍《Atomic Scala》。这本书旨在帮助读者深入理解Scala的编程理念和语法特性,通过实例化学习,让初学者和经验丰富的开发者都能从中受益。这...

    Beginning Scala Apress 2ed 2015

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

    Java EE 7 tutorial 中的examples代码

    1. **Servlet 3.1**:Java EE 7中Servlet规范升级到3.1版,引入了异步处理能力,允许Servlet在不阻塞请求的情况下执行长时间运行的任务。同时,Servlet 3.1还支持WebSocket协议,为实时通信提供了可能。 2. **JSP ...

    Scala and Spark for Big Data Analytics

    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.

    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西门子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 programming-scala-1st-edition-code-examples

    algorithm design foundations analysis and internet examples 中文版

    algorithm design foundations analysis and internet examples 中文版

    Probability: Theory and Examples

    这本书《Probability: Theory and Examples》是由Rick Durrett所著的概率论领域的重要学术作品。在本书中,作者深入探讨了概率论的基本理论及其应用实例,涵盖了概率空间、分布、随机变量、积分理论、期望值计算、...

    [Durrett] Probability Theory and Examples Solutions.pdf

    标题《[Durrett] Probability Theory and Examples Solutions.pdf》和描述“概率论”指明了文档内容与概率论领域紧密相关,特别是《概率论与例题解答》一书的第二版中解决方案的详细讨论。这本书的第二版增加了解决...

    types and programming languages

    each new concept is motivated by programming examples and the more theoretical sections are driven by the needs of implementations. Each chapter is accompanied by numerous exercises and solutions, as...

    Probability:Theory and Examples.pdf

    概率论非常经典的书籍,包括大数定律、中心极限定理、随机游动、鞅、马尔可夫链、遍历定理和布朗运动。它是一种综合性的处理方法,集中在对应用最有用的结果上。它的哲学是,学习概率的最好方法是看到它的实际行动,...

    Real World Functional Programming: With Examples in F# and C#

    Functional programming languages like F#, Erlang, and Scala are attracting attention as an efficient way to handle the new requirements for programming multi-processor and high-availability ...

    Scala By Examples

    "Expressions and Simple Functions"章节深入讲解了Scala的表达式和函数。在Scala中,一切皆为表达式,这意味着每个代码块都有一个值。这一章会涉及表达式的计算规则、参数传递、条件表达式(如if-else)的用法,...

    PTB数据集simple-examples.zip

    PTB数据集simple-examples.zip 妾发初覆额,折花门前剧。 郎骑竹马来,绕床弄青梅。 同居长干里,两小无嫌猜, 十四为君妇,羞颜未尝开。 低头向暗壁,千唤不一回。 十五始展眉,愿同尘与灰。 常存抱柱信,岂上...

Global site tag (gtag.js) - Google Analytics