You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Classes in Scala are static templates that can be instantiated into many objects at runtime.
15
-
Here is a class definition which defines a class `Point`:
16
+
Classes in Scala are blueprints for creating objects. They can contain methods,
17
+
values, and variables, which are collectively called _members_.
18
+
19
+
## Defining a class
20
+
A minimal class definition is simply the keyword `class` and
21
+
an identifier:
22
+
```tut
23
+
class User
24
+
25
+
val user1 = new User
26
+
```
27
+
The keyword `new` is used to create an instance of the class. `User` has an implicit constructor which takes no arguments because no constructor was defined. However, you'll often want a constructor and class body. Here is an example class definition for a point:
16
28
17
29
```tut
18
30
class Point(var x: Int, var y: Int) {
31
+
19
32
def move(dx: Int, dy: Int): Unit = {
20
33
x = x + dx
21
34
y = y + dy
22
35
}
36
+
23
37
override def toString: String =
24
-
"(" + x + ", " + y + ")"
38
+
s"($x, $y)"
25
39
}
40
+
41
+
val point1 = new Point(2, 3)
42
+
point1.x // 2
43
+
println(point1) // prints (x, y)
26
44
```
27
45
28
-
Classes in Scala are parameterized with constructor arguments. The code above defines two constructor arguments, `x` and `y`; they are both visible in the whole body of the class.
46
+
This `Point` class has four members: the variables `x` and `y` and the methods `move` and
47
+
`toString`. Unlike many other languages, the primary constructor is in the class signature `(var x: Int, var y: Int)`. The `move` method takes two integer arguments but does not return a value (the return type `Unit` corresponds to `void` in Java-like languages). `toString`, on the other hand, does not take any parameters but returns a `String` value. Since `toString` overrides the inherited `toString` method, it is tagged with the `override` keyword.
48
+
49
+
## Constructors
50
+
51
+
Constructors can have optional parameters by providing a default value like so:
29
52
30
-
The class also includes two methods, `move` and `toString`. `move` takes two integer arguments but does not return a value (the return type `Unit` corresponds to `void` in Java-like languages). `toString`, on the other hand, does not take any parameters but returns a `String` value. Since `toString` overrides the pre-defined `toString` method, it is tagged with the `override` keyword.
53
+
```tut
54
+
class Point(var x: Int = 0, var y: Int = 0)
31
55
32
-
Note that in Scala, it isn't necessary to say `return` in order to return a value. The value returned from a method is simply the last value in the method body. In the case of the `toString` method above, the expression after the equals sign is evaluated and returned to the caller.
56
+
val origin = new Point // x and y are both set to 0
57
+
val point1 = new Point(1)
58
+
println(point1.x) // prints 1
33
59
34
-
Classes are instantiated with the `new` primitive, as follows:
60
+
```
35
61
62
+
In this version of the `Point` class, `x` and `y` have the default value `0` so no arguments are required. However, because the constructor reads arguments left to right, if you just wanted to pass in a `y` value, you would need to name the parameter.
63
+
```
64
+
class Point(var x: Int = 0, var y: Int = 0)
65
+
val point2 = new Point(y=2)
66
+
println(point2.y) // prints 2
67
+
```
68
+
69
+
This is also a good practice to enhance clarity.
70
+
71
+
## Member scope
72
+
Members are public by default. Use the `private` access modifier
73
+
to hide them from outside of the class.
36
74
```tut
37
-
object Classes {
38
-
def main(args: Array[String]) {
39
-
val pt = new Point(1, 2)
40
-
println(pt)
41
-
pt.move(10, 10)
42
-
println(pt)
75
+
class Point {
76
+
private var _x = 0
77
+
private var _y = 0
78
+
private val bound = 100
79
+
80
+
def x = _x
81
+
def x_= (newValue: Int): Unit = {
82
+
if (newValue < bound) _x = newValue else printWarning
83
+
}
84
+
85
+
def y = _y
86
+
def y_= (newValue: Int): Unit = {
87
+
if (newValue < bound) _y = newValue else printWarning
43
88
}
89
+
90
+
private def printWarning = println("WARNING: Out of bounds")
44
91
}
45
-
```
46
92
47
-
The program defines an executable application Classes in form of a top-level [singleton object](singleton-objects) with a `main` method. The `main` method creates a new `Point` and stores it in value `pt`. Note that values defined with the `val` construct are different from variables defined with the `var` construct (see class `Point` above) in that they do not allow updates; i.e. the value is constant.
93
+
val point1 = new Point
94
+
point1.x = 99
95
+
point1.y = 101 // prints the warning
96
+
```
97
+
In this version of the `Point` class, the data is stored in private variables `_x` and `_y`. There are methods `def x` and `def y` for accessing the private data. `def x_=` and `def y_=` are for validating and setting the value of `_x` and `_y`. Notice the special syntax for the setters: the method has `_=` appended to the identifier of the getter and the parameters come after.
48
98
49
-
Here is the output of the program:
99
+
Primary constructor parameters with `val` and `var` are public. However, because `val`s are immutable, you can't write the following.
100
+
```
101
+
class Point(val x: Int, val y: Int)
102
+
val point = new Point(1, 2)
103
+
val.x = 3 // <-- does not compile
104
+
```
50
105
106
+
Parameters without `val` or `var` are private values, visible only within the class.
0 commit comments