Recently I transit to use scala to program.

scala is a functional and objected oriented language, but it has seamless java Interoperability  (they both run in JVM and freely mixed).

Compared to the java that I am familiar to,  there are some common concepts, data structure functions I often use in Scala,

They are also some kinds of distinctions from Java object oriented language.  I put here also for quick search afterwards.

 
(1)  var:  define variable;
       val: deine a constant
       e.g.
       var i = 0;      i = i + 1        // i can be changed
       val i: Int = 0       //i value is not allowed to change
 
(2) object
Everything is object;         
e.g.  even basic data structure Int   are interpreted as  abstract final class Int
 
(3)  difference between object  and  class:
Simple differences:
Object:  A singleton is a class that can have only one instance, it is like the static field and method in  the java class , but it can extend another superclass, implement interfaces,
Class is  that you can have multiple instances of a class.
 e.g.
object A extends B with C {
def f(x: Any): Any = ???
}
It declares an anonymous (inaccessible) class that extends both B and C, and creates a single instance of this class named A.
(4) Option/Some/None pattern
Scala uses option to avoid the null or null pointer problem in Java etc. 
it use values that may be present or not:  the Option[A] trait.
Some extends Option, so it inherits everything except get and isEmpty (and some other methods implemented by a case class).
None also extend option
In a word,
 Option
/ \
/ \
/ \
Some None
Option is container base which can be empty or full
While Some(x) represent full with 'x' being present in the container, None represents empty.
val a: Option[String] = Option(null) // a will be None
val b: Option[String] = Option("Hello!") // "hello"
 
(4) trait
       Similar to java's interface, it encapsulates method and field definitions. It can also be used to define object types by specifying the signature of the supported methods.
example:
 
trait Equal {
def isEqual(x: Any): Boolean
def isNotEqual(x: Any): Boolean = !isEqual(x)
} class Point(xc: Int, yc: Int) extends Equal {
var x: Int = xc
var y: Int = yc def isEqual(obj: Any) = obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == y
}

(5)  case class 

It defines abstract or concrete properties in an abstract base class (or trait) that can be referenced in all child classes.
Case classes are compared by structure and not by reference:
case class Message(sender: String, recipient: String, body: String)
val message1 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?")
 
You can create a deep copy of an instance of a case class simply by using the copy method. You can optionally change the constructor arguments.
val message2 = message1.copy(sender = message4.recipient, recipient = "claire@bourgogne.fr")
 It can be used to construct the struct  data structure in c/c++
 
reference:
 
 

Scala note 1的更多相关文章

  1. Spark开发环境搭建(IDEA、Scala、SVN、SBT)

    软件版本 软件信息 软件名称 版本 下载地址 备注 Java 1.8 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-dow ...

  2. How to merge Scala Lists

    Scala List FAQ: How do I merge a List in Scala? NOTE: I wrote the solutions shown below a long time ...

  3. Scala: Types of a higher kind

    One of the more powerful features Scala has is the ability to generically abstract across things tha ...

  4. <译>Spark Sreaming 编程指南

    Spark Streaming 编程指南 Overview A Quick Example Basic Concepts Linking Initializing StreamingContext D ...

  5. Beginning Scala study note(9) Scala and Java Interoperability

    1. Translating Java Classes to Scala Classes Example 1: # a class declaration in Java public class B ...

  6. Beginning Scala study note(8) Scala Type System

    1. Unified Type System Scala has a unified type system, enclosed by the type Any at the top of the h ...

  7. Beginning Scala study note(7) Trait

    A trait provides code reusability in Scala by encapsulating method and state and then offing possibi ...

  8. Beginning Scala study note(6) Scala Collections

    Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...

  9. Beginning Scala study note(5) Pattern Matching

    The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...

随机推荐

  1. JavaScript中 DOM操作方法

    DM是(Document Object Model)的简称. 一.找元素 document.getElementById()    根据id选择器找,最多找一个: document.getElemen ...

  2. Linux防火墙配置—SNAT1

    1.实验目标 以实验"防火墙配置-访问外网WEB"为基础,在WEB服务器上安装Wireshark,设置Wireshark的过滤条件为捕获HTTP报文,在Wireshark中开启捕获 ...

  3. Filter和Listener的应用——分IP统计网站访问次数

    一:分析 统计工作需要在所有资源执行前进行,所以需要放在filter中 这个拦截器仅仅进行统计工作,不进行拦截,所以请求必须继续传递下去 用Map<String,integer>来保存数据 ...

  4. 内嵌的Component调用外部的方法

    如果一个内嵌的Component控件需要调用外部定义的方法,用outerDocument.方法名来调用,前提是该方法是public的.如:<mx:DataGridColumn headerTex ...

  5. jquery中is()函数

    is(expr)函数判断当前Jquery对象所匹配的元素是否存在.只要其中一种符合,就返回 true,否则返回 false. 如果 expr是个字符串,既视为Jquery的选择器,用于表示选择的元素. ...

  6. 蓝桥杯-大衍数列-java

    /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...

  7. Maven的简单搭建

    Maven这个个项目管理和构建自动化工具,越来越多的开发人员使用它来管理项目中的jar包.接下来将从下面几个方向介绍maven: (1)Maven简单介绍 (2)Maven安装与配置 (3)Maven ...

  8. js,jQuery和DOM操作的总结(二)

    jQuery的基本操作 (1)遍历键值对和数组 , , , , , ]; $.map(arr, function (ele, index) { alert(ele + '===' + index); ...

  9. 让Unity的Inspector面板支持字符限制(restrict)功能

    今天在优化红点组件,笔者打算将红点id由10进制改为16进制处理,就打算将红点id字段由uint类型改成string类型,用于填写16进制的字符(因为在Inspector面板里,uint/int类型字 ...

  10. C/s从文件(TXT)中读取数据插入数据库

    流程: 1.当按钮单击时,弹出OpenFileDialog 2.判断后缀名是否合法 3.导入数据库 按钮事件中的代码: 1.判断用户是否选中文件. 2.判断用户选择的文件是否为txt //第一步,当按 ...