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 class vector has a method `+` which we used to add vector1 and vector2. When the method has two or more parameters, use a tuple:
37
+
```
38
+
class Rectangle(val x: Double, val y: Double) {
39
+
def changeSize(dx: Int, dy: Int): Rectangle = new Rectangle(x + dx, y + dy)
40
+
}
41
+
42
+
val rect1 = new Rectangle(1, 2)
43
+
val rect2 = rect1 changeSize (2, 2) // equivalent to rect1.changeSize(2, 2)
44
+
```
13
45
14
-
Any method which takes a single parameter can be used as an *infix operator* in Scala. Here is the definition of class `MyBool` which includes methods `and` and `or`:
46
+
Using parentheses, you can build up complex expressions with readable syntax. Here is the definition of class `MyBool` which includes methods `and` and `or`:
15
47
16
48
```tut
17
49
case class MyBool(x: Boolean) {
@@ -28,11 +60,4 @@ def not(x: MyBool) = x.negate
28
60
def xor(x: MyBool, y: MyBool) = (x or y) and not(x and y)
29
61
```
30
62
31
-
This helps to make the definition of `xor` more readable.
32
-
33
-
Here is the corresponding code in a more traditional object-oriented programming language syntax:
0 commit comments