Modified ex 3 in class delegation
This commit is contained in:
parent
cc9bf98a21
commit
385e1c36e3
|
@ -1,57 +1,46 @@
|
|||
// ClassDelegation/ClassDelegEx3.kt
|
||||
package classDelegationExercise3
|
||||
import atomictest.*
|
||||
|
||||
interface A {
|
||||
val x: Int
|
||||
val z: Int
|
||||
fun u()
|
||||
fun v()
|
||||
fun foo()
|
||||
fun bar()
|
||||
}
|
||||
|
||||
interface B {
|
||||
val y: Int
|
||||
val z: Int
|
||||
fun v()
|
||||
fun w()
|
||||
fun foo()
|
||||
fun baz()
|
||||
}
|
||||
|
||||
class AA : A {
|
||||
override val x = 1
|
||||
override val z = 1
|
||||
override fun u() = trace("AA.u()")
|
||||
override fun v() = trace("AA.v()")
|
||||
override fun foo() = trace("AA.foo()")
|
||||
override fun bar() = trace("AA.bar()")
|
||||
}
|
||||
|
||||
class BB : B {
|
||||
override val y = 1
|
||||
override val z = 1
|
||||
override fun v() = trace("BB.v()")
|
||||
override fun w() = trace("BB.w()")
|
||||
override fun foo() = trace("BB.foo()")
|
||||
override fun baz() = trace("BB.baz()")
|
||||
}
|
||||
|
||||
class Delegation(val a: A, val b: B) :
|
||||
A by a, B by b {
|
||||
override val z = a.z + b.z
|
||||
override fun v() {
|
||||
trace("Delegation.v()")
|
||||
a.v()
|
||||
b.v()
|
||||
trace("Delegation.z: $z")
|
||||
override fun foo() {
|
||||
a.foo()
|
||||
b.foo()
|
||||
trace("Delegation.foo()")
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val d = Delegation(AA(), BB())
|
||||
d.u()
|
||||
d.v()
|
||||
d.w()
|
||||
trace eq """
|
||||
AA.u()
|
||||
Delegation.v()
|
||||
AA.v()
|
||||
BB.v()
|
||||
Delegation.z: 2
|
||||
BB.w()
|
||||
"""
|
||||
d.foo()
|
||||
d.bar()
|
||||
d.baz()
|
||||
trace eq
|
||||
"""
|
||||
AA.foo()
|
||||
BB.foo()
|
||||
Delegation.foo()
|
||||
AA.bar()
|
||||
BB.baz()
|
||||
"""
|
||||
}
|
|
@ -2,5 +2,24 @@ type: edu
|
|||
files:
|
||||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 136
|
||||
length: 98
|
||||
placeholder_text: class AA
|
||||
- offset: 236
|
||||
length: 98
|
||||
placeholder_text: class BB
|
||||
- offset: 336
|
||||
length: 140
|
||||
placeholder_text: class Delegation
|
||||
- offset: 491
|
||||
length: 62
|
||||
placeholder_text: |-
|
||||
/*
|
||||
val d = Delegation(AA(), BB())
|
||||
d.foo()
|
||||
d.bar()
|
||||
d.baz()
|
||||
*/
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -1,26 +1,10 @@
|
|||
## Class Delegation (#3)
|
||||
|
||||
What happens when you delegate to two interfaces that have one or more
|
||||
functions or fields in common? Start with two interfaces:
|
||||
members in common? The started code contains two interfaces `A` and `B`
|
||||
that both have `foo()` member function.
|
||||
|
||||
```kotlin
|
||||
interface A {
|
||||
val x: Int
|
||||
val z: Int
|
||||
fun u()
|
||||
fun v()
|
||||
}
|
||||
|
||||
interface B {
|
||||
val y: Int
|
||||
val z: Int
|
||||
fun v()
|
||||
fun w()
|
||||
}
|
||||
```
|
||||
|
||||
Create a class `AA` that implements `A`, producing the value `1` for `x` and
|
||||
`z`, and tracing `"AA.u()"` for `u()` and `"AA.v()"` for `v()`. Create a
|
||||
similar implementation `BB` implementing `B`. Now create a class `Delegation`
|
||||
which delegates to both `A` and `B`. IntelliJ or the compiler will guide you in
|
||||
resolving the collisions.
|
||||
Create a class `AImpl` that implements `A`, that traces `"A.foo()"` for `foo()`
|
||||
and `"A.bar()"` for `bar()`. Create a similar implementation `BImpl`
|
||||
implementing `B`. Now create a class `Delegation` which delegates to both `A`
|
||||
and `B`. IntelliJ or the compiler will guide you in resolving the collisions.
|
Loading…
Reference in New Issue