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
The keyword `new` is used to create an instance of the class. `User` has a default 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:
27
30
31
+
The keyword `new` is used to create an instance of the class.
32
+
{% endtab %}
33
+
34
+
{% tab 'Scala 3' for=class-minimal-user %}
35
+
```scala
36
+
classUser
37
+
38
+
valuser1=User()
39
+
```
40
+
41
+
We call the class like a function, as `User()`, to create an instance of the class.
42
+
It is also possible to explicitly use the `new` keyword, as `new User()`, although that is usually left out.
43
+
{% endtab %}
44
+
45
+
{% endtabs %}
46
+
47
+
`User` has a default 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:
@@ -38,76 +62,203 @@ class Point(var x: Int, var y: Int) {
38
62
}
39
63
40
64
valpoint1=newPoint(2, 3)
41
-
println(point1.x) // 2
42
-
println(point1) // prints (2, 3)
65
+
println(point1.x) // prints 2
66
+
println(point1) // prints (2, 3)
67
+
```
68
+
{% endtab %}
69
+
70
+
{% tab 'Scala 3' for=class-point-example %}
71
+
```scala
72
+
classPoint(varx:Int, vary:Int):
73
+
74
+
defmove(dx: Int, dy: Int):Unit=
75
+
x = x + dx
76
+
y = y + dy
77
+
78
+
overridedeftoString:String=
79
+
s"($x, $y)"
80
+
endPoint
81
+
82
+
valpoint1=Point(2, 3)
83
+
println(point1.x) // prints 2
84
+
println(point1) // prints (2, 3)
43
85
```
86
+
{% endtab %}
87
+
88
+
{% endtabs %}
44
89
45
90
This `Point` class has four members: the variables `x` and `y` and the methods `move` and
46
-
`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 and returns the Unit value `()`, which carries no information. This corresponds roughly with`void` in Java-like languages. `toString`, on the other hand, does not take any arguments but returns a `String` value. Since `toString` overrides `toString` from [`AnyRef`](unified-types.html), it is tagged with the `override` keyword.
91
+
`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 and returns the Unit value `()`, which carries no information. This corresponds roughly to`void` in Java-like languages. `toString`, on the other hand, does not take any arguments but returns a `String` value. Since `toString` overrides `toString` from [`AnyRef`](unified-types.html), it is tagged with the `override` keyword.
47
92
48
93
## Constructors
49
94
50
95
Constructors can have optional parameters by providing a default value like so:
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.
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.
97
218
98
219
Primary constructor parameters with `val` and `var` are public. However, because `val`s are immutable, you can't write the following.
0 commit comments