Latest web development tutorials

Scala classes and objects

Class is an abstract object, and the object is a specific instance of a class. Class is abstract and does not take up memory, and the object is concrete, take up storage space. Class is a blueprint for creating an object, it is a software-defined templates include methods and variables in specific types of objects in the.

We can use the new keyword to create objects of the class, examples are as follows:

class Point(xc: Int, yc: Int) {
   var x: Int = xc
   var y: Int = yc

   def move(dx: Int, dy: Int) {
      x = x + dx
      y = y + dy
      println ("x 的坐标点: " + x);
      println ("y 的坐标点: " + y);
   }
}

Scala in the class are not declared as public, a Scala source file can have multiple classes.

Examples of the above class defines two variablesx and y,amethod:move, the method has no return value.

Class definition Scala can have parameters, called class parameters, such as the above xc, yc, class parameters in the whole class can access.

Then we can use new to instantiate the class and access class methods and variables:

import java.io._

class Point(xc: Int, yc: Int) {
   var x: Int = xc
   var y: Int = yc

   def move(dx: Int, dy: Int) {
      x = x + dx
      y = y + dy
      println ("x 的坐标点: " + x);
      println ("y 的坐标点: " + y);
   }
}

object Test {
   def main(args: Array[String]) {
      val pt = new Point(10, 20);

      // 移到一个新的位置
      pt.move(10, 10);
   }
}

Implementation of the above code, the output is:

$ scalac Test.scala 
$ scala Test
x 的坐标点: 20
y 的坐标点: 30

Scala inherit

Scala inherit a base class with Java is very similar, but we need to pay attention to the following points:

  • 1, rewrite a non-abstract method must use the override modifier.
  • 2, is available in the past only the main constructor of the base class constructor write parameters.
  • 3. When overridden in a subclass the abstract superclass, you do not need to use the override keyword.

Let's look at an instance:

class Point(xc: Int, yc: Int) {
   var x: Int = xc
   var y: Int = yc

   def move(dx: Int, dy: Int) {
      x = x + dx
      y = y + dy
      println ("x 的坐标点: " + x);
      println ("y 的坐标点: " + y);
   }
}

class Location(override val xc: Int, override val yc: Int,
   val zc :Int) extends Point(xc, yc){
   var z: Int = zc

   def move(dx: Int, dy: Int, dz: Int) {
      x = x + dx
      y = y + dy
      z = z + dz
      println ("x 的坐标点 : " + x);
      println ("y 的坐标点 : " + y);
      println ("z 的坐标点 : " + z);
   }
}

Scala use extends keyword to inherit a class. Instances Location class inherits Point class. Point called the parent class (base class), Location is called a subclass.

override val xc rewrite the parent class field.

All properties and methods inherit will inherit the parent class, Scala allows only inherit a parent class.

Examples are as follows:

import java.io._

class Point(val xc: Int, val yc: Int) {
   var x: Int = xc
   var y: Int = yc
   def move(dx: Int, dy: Int) {
      x = x + dx
      y = y + dy
      println ("x 的坐标点 : " + x);
      println ("y 的坐标点 : " + y);
   }
}

class Location(override val xc: Int, override val yc: Int,
   val zc :Int) extends Point(xc, yc){
   var z: Int = zc

   def move(dx: Int, dy: Int, dz: Int) {
      x = x + dx
      y = y + dy
      z = z + dz
      println ("x 的坐标点 : " + x);
      println ("y 的坐标点 : " + y);
      println ("z 的坐标点 : " + z);
   }
}

object Test {
   def main(args: Array[String]) {
      val loc = new Location(10, 20, 15);

      // 移到一个新的位置
      loc.move(10, 10, 5);
   }
}

Implementation of the above code, the output is:

$ scalac Test.scala 
$ scala Test
x 的坐标点 : 20
y 的坐标点 : 30
z 的坐标点 : 20

Scala rewrite a non-abstract method must override modifier.

class Person {
  var name = ""
  override def toString = getClass.getName + "[name=" + name + "]"
}

class Employee extends Person {
  var salary = 0.0
  override def toString = super.toString + "[salary=" + salary + "]"
}

object Test extends App {
  val fred = new Employee
  fred.name = "Fred"
  fred.salary = 50000
  println(fred)
}

Implementation of the above code, the output is:

$ scalac Test.scala 
$ scala Test
Employee[name=Fred][salary=50000.0]

Scala singleton object

In Scala, this thing is not static, but it also provides us with a method to achieve Singleton pattern, and that is to use the keyword object.

Scala when used in single-case model, in addition to the class definition, but also the definition of a target object with the same name, is the difference between it and the class, object object can not take parameters.

When Singleton objects share the same name with a class, he is called companion objects of this class: companion object. You must define the class and its associated objects in the same source file. This class is called the singleton object accompanying class: companion class. Class and its companion object can access each other their private members.

Singleton object instances

import java.io._

class Point(val xc: Int, val yc: Int) {
   var x: Int = xc
   var y: Int = yc
   def move(dx: Int, dy: Int) {
      x = x + dx
      y = y + dy
   }
}

object Test {
   def main(args: Array[String]) {
      val point = new Point(10, 20)
      printPoint

      def printPoint{
         println ("x 的坐标点 : " + point.x);
         println ("y 的坐标点 : " + point.y);
      }
   }
}

Implementation of the above code, the output is:

$ scalac Test.scala 
$ scala Test
x 的坐标点 : 10
y 的坐标点 : 20

Associated object instance

/* 文件名:Marker.scala
 * author:本教程
 * url:www.w3big.com
 */

// 私有构造方法
class Marker private(val color:String) {

  println("创建" + this)
  
  override def toString(): String = "颜色标记:"+ color
  
}

// 伴生对象,与类共享名字,可以访问类的私有属性和方法
object Marker{
  
    private val markers: Map[String, Marker] = Map(
      "red" -> new Marker("red"),
      "blue" -> new Marker("blue"),
      "green" -> new Marker("green")
    )
    
    def apply(color:String) = {
      if(markers.contains(color)) markers(color) else null
    }
  
    
    def getMarker(color:String) = { 
      if(markers.contains(color)) markers(color) else null
    }
    def main(args: Array[String]) { 
        println(Marker("red"))  
        // 单例函数调用,省略了.(点)符号  
		println(Marker getMarker "blue")  
    }
}

Implementation of the above code, the output is:

$ scalac Marker.scala 
$ scala Marker
创建颜色标记:red
创建颜色标记:blue
创建颜色标记:green
颜色标记:red
颜色标记:blue