Scala Classes and Objects
A class is an abstraction of objects, while an object is a concrete instance of a class. Classes are abstract and do not occupy memory, whereas objects are concrete and occupy storage space. A class is a blueprint for creating objects; it is a software template that defines the methods and variables included in objects of a particular type.
We can use the new keyword to create an object of a class, as shown in the following example:
Example
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("x coordinate point: " + x);
println ("y coordinate point: " + y);
}
}
In Scala, classes are not declared as public; a single Scala source file can contain multiple classes.
The class in the above example defines two variablesxandy, and a method:move, the method has no return value.
Scala class definitions can have parameters, called class parameters, such as xc and yc above. Class parameters are accessible throughout the entire class.
Then we can use new to instantiate the class and access the methods and variables in the class:
Example
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 coordinate point: " + x);
println ("y coordinate point: " + y);
}
}
object Test {
def main(args: Array[String]) {
val pt = new Point(10, 20);
// Move to a new location
pt.move(10, 10);
}
}
Executing the above code produces the following output:
$ scalac Test.scala $ scala Test x 的坐标点: 20 y 的坐标点: 30
Scala Inheritance
Scala inheriting a base class is very similar to Java, but we need to note the following points:
- 1. Overriding a non-abstract method must use the override modifier.
- 2. Only the primary constructor can pass parameters to the base class constructor.
- 3. When overriding an abstract method of a superclass in a subclass, you do not need to use the override keyword.
Next, let's look at an example:
Example
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("x coordinate point: " + x);
println ("y coordinate point: " + 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 coordinate point : " + x);
println ("y coordinate point : " + y);
println ("z coordinate point : " + z);
}
}
Scala uses the extends keyword to inherit a class. In the example, the Location class inherits the Point class. Point is called the parent class (base class), and Location is called the subclass.
override val xcThis is to override the fields of the parent class.
Inheritance inherits all the properties and methods of the parent class. Scala only allows inheriting from one parent class.
An example is as follows:
Example
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 coordinate point : " + x);
println ("y coordinate point : " + 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 coordinate point : " + x);
println ("y coordinate point : " + y);
println ("z coordinate point : " + z);
}
}
object Test {
def main(args: Array[String]) {
val loc = new Location(10, 20, 15);
// Move to a new location
loc.move(10, 10, 5);
}
}
Executing the above code produces the following output:
$ scalac Test.scala $ scala Test x 的坐标点 : 20 y 的坐标点 : 30 z 的坐标点 : 20
In Scala, overriding a non-abstract method must use the override modifier.
Example
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)
}
Executing the above code produces the following output:
$ scalac Test.scala $ scala Test Employee[name=Fred][salary=50000.0]
Scala Singleton Objects
In Scala, there is no such thing as static, but it still provides us with a way to implement the singleton pattern, which is to use the keyword object.
When using the singleton pattern in Scala, in addition to the defined class, you also need to define an object with the same name. The difference between it and a class is that an object cannot have parameters.
When a singleton object shares the same name with a class, it is called the companion object of that class. You must define the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access each other's private members.
Singleton Object Example
Example
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 coordinate point : " + point.x);
println ("y coordinate point : " + point.y);
}
}
}
Executing the above code produces the following output:
$ scalac Test.scala $ scala Test x 的坐标点 : 10 y 的坐标点 : 20
Companion Object Example
Example
* author: Example
* url:www.example.com
*/
// Private constructor
class Marker private(val color:String) {
println("Create" + this)
override def toString(): String = "Color marker: "+ color
}
// Companion object, with the same name as the class, can access the class's private properties and methods
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"))
// Singleton function call, omitting the . (dot) symbol
println(Marker getMarker "blue")
}
}
Executing the above code produces the following output:
$ scalac Marker.scala $ scala Marker 创建颜色标记:red 创建颜色标记:blue 创建颜色标记:green 颜色标记:red 颜色标记:blueOther Extensions