From 385e1c36e32c3f472e9f474e69cdec8558787e6e Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Fri, 31 Jul 2020 14:56:31 +0200 Subject: [PATCH] Modified ex 3 in class delegation --- .../Class Delegation/Exercise 3/src/Task.kt | 57 ++++++++----------- .../Exercise 3/task-info.yaml | 19 +++++++ .../Class Delegation/Exercise 3/task.md | 28 ++------- 3 files changed, 48 insertions(+), 56 deletions(-) diff --git a/Object-Oriented Programming/Class Delegation/Exercise 3/src/Task.kt b/Object-Oriented Programming/Class Delegation/Exercise 3/src/Task.kt index 0ebf1c30..55e754f4 100644 --- a/Object-Oriented Programming/Class Delegation/Exercise 3/src/Task.kt +++ b/Object-Oriented Programming/Class Delegation/Exercise 3/src/Task.kt @@ -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() + """ } \ No newline at end of file diff --git a/Object-Oriented Programming/Class Delegation/Exercise 3/task-info.yaml b/Object-Oriented Programming/Class Delegation/Exercise 3/task-info.yaml index 7ad1b827..9643c42d 100644 --- a/Object-Oriented Programming/Class Delegation/Exercise 3/task-info.yaml +++ b/Object-Oriented Programming/Class Delegation/Exercise 3/task-info.yaml @@ -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 diff --git a/Object-Oriented Programming/Class Delegation/Exercise 3/task.md b/Object-Oriented Programming/Class Delegation/Exercise 3/task.md index 85bdf15f..1cbf5399 100644 --- a/Object-Oriented Programming/Class Delegation/Exercise 3/task.md +++ b/Object-Oriented Programming/Class Delegation/Exercise 3/task.md @@ -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. \ No newline at end of file +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. \ No newline at end of file