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
@@ -30,19 +33,60 @@ println(d.loudMessage) // I'M AN INSTANCE OF CLASS B
30
33
```
31
34
Class `D` has a superclass `B` and a mixin `C`. Classes can only have one superclass but many mixins (using the keywords `extends` and `with` respectively). The mixins and the superclass may have the same supertype.
32
35
36
+
{% endtab %}
37
+
38
+
{% tab 'Scala 3' for=mixin-first-exemple %}
39
+
```scala
40
+
abstractclassA:
41
+
valmessage:String
42
+
classBextendsA:
43
+
valmessage="I'm an instance of class B"
44
+
traitCextendsA:
45
+
defloudMessage= message.toUpperCase()
46
+
classDextendsB, C
47
+
48
+
vald=D()
49
+
println(d.message) // I'm an instance of class B
50
+
println(d.loudMessage) // I'M AN INSTANCE OF CLASS B
51
+
```
52
+
Class `D` has a superclass `B` and a mixin `C`. Classes can only have one superclass but many mixins (using the keyword `extends` and the separator `,` respectively). The mixins and the superclass may have the same supertype.
53
+
54
+
{% endtab %}
55
+
56
+
{% endtabs %}
57
+
33
58
Now let's look at a more interesting example starting with an abstract class:
This trait implements `foreach` by continually calling the provided function `f: T => Unit` on the next element (`next()`) as long as there are further elements (`while (hasNext)`). Because `RichIterator` is a trait, it doesn't need to implement the abstract members of AbsIterator.
defforeach(f: T=>Unit):Unit=while hasNext do f(next())
139
+
}
140
+
```
141
+
This trait implements `foreach` by continually calling the provided function `f: T => Unit` on the next element (`next()`) as long as there are further elements (`while hasNext`). Because `RichIterator` is a trait, it doesn't need to implement the abstract members of AbsIterator.
142
+
143
+
{% endtab %}
144
+
145
+
{% endtabs %}
146
+
69
147
We would like to combine the functionality of `StringIterator` and `RichIterator` into a single class.
0 commit comments