Scala Classes and Objects
A class is an abstraction of an object, while an object is a concrete instance of a class. A class is abstract and occupies no memory, whereas an object is concrete and occupies 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 below:
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: "
- x
)
;
println
(
"y coordinate: "
- y
)
;
}
}
Classes in Scala are not declared as public, and a single Scala source file can contain multiple classes.
The class in the above example defines two variables x and y , and one method:move, and the method has no return value.
A Scala class definition 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 its methods and variables:
Example
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 coordinate: "
- x
)
;
println
(
"y coordinate: "
- y
)
;
}
}
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
val
pt
=
new
Point
(
10
,
20
)
;
// move to a new location
pt.
move
(
10
,
10
)
;
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
x 的坐标点: 20
y 的坐标点: 30
Scala Inheritance
Inheriting a base class in Scala is very similar to Java, but we need to note the following points:
- 1. To override a non-abstract method, you must use the override modifier.
- 2. Only the primary constructor can pass parameters to the base class's 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
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: "
- x
)
;
println
(
"y coordinate: "
- 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 : "
- x
)
;
println
(
"y coordinate : "
- y
)
;
println
(
"z coordinate : "
- z
)
;
}
}
Scala uses the extends keyword to inherit from a class. In the example, the Location class inherits from the Point class. Point is called the parent class (base class), and Location is called the subclass.
override val xc overrides a field of the parent class.
Inheritance inherits all properties and methods of the parent class, and Scala only allows inheriting from one parent class.
The example is as follows:
Example
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 coordinate : "
- x
)
;
println
(
"y coordinate : "
- 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 : "
- x
)
;
println
(
"y coordinate : "
- y
)
;
println
(
"z coordinate : "
- 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
)
;
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
x 的坐标点 : 20
y 的坐标点 : 30
z 的坐标点 : 20
To override a non-abstract method in Scala, you must use the override modifier.
Example
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
)
}
The output of the code above is:
$ 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 provides a way to implement the singleton pattern, which is by using the object keyword.
When using the singleton pattern in Scala, in addition to defining the class, you must define an object with the same name. The difference between the object and the class is that the object cannot take parameters.
When a singleton object shares the same name as 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
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 coordinate : "
- point.
x
)
;
println
(
"y coordinate : "
- point.
y
)
;
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
x 的坐标点 : 10
y 的坐标点 : 20
Companion Object Example
Example
/* File name: Marker.scala
* author:Runoob Tutorial
* url:www.runoob.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"
)
}
}
The output of the code above is:
$ scalac Marker.scala
$ scala Marker
创建颜色标记:red
创建颜色标记:blue
创建颜色标记:green
颜色标记:red
颜色标记:blue