Scala note 1

Posted 安新

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scala note 1相关的知识,希望对你有一定的参考价值。

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
 
(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("[email protected]", "[email protected]", "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 = "[email protected]")
 It can be used to construct the struct  data structure in c/c++
 
reference:
 
 

以上是关于Scala note 1的主要内容,如果未能解决你的问题,请参考以下文章

Beginning Scala study note Basics of Scala

Scala note 1

Beginning Scala study note Functional Programming in Scala

Beginning Scala study note Scala Type System

Beginning Scala study note Scala and Java Interoperability

[Notes] 简单的 Scala 尾递归函数