Minor: wording improvements
This commit is contained in:
parent
75d281ac02
commit
2cf5ba2cd9
|
@ -10,7 +10,7 @@ class ButtonImage(
|
|||
val width: Int,
|
||||
val height: Int
|
||||
): Rectangle {
|
||||
override fun paint() =
|
||||
override fun paint() =
|
||||
"painting ButtonImage($width, $height)"
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
## Class Delegation (#2)
|
||||
|
||||
Exercise 1 in [Inheritance & Extensions] uses
|
||||
composition to adapt `Crocodile` to work with `interactWithDuck`.
|
||||
This produces an inconsistency when using `IAmHonestlyDuck` with the
|
||||
`interactWithCrocodile()` function: the composed `crocodile` must be explicitly
|
||||
named during the call:
|
||||
composition to adapt `Crocodile` to work with `interactWithDuck()`. This
|
||||
produces an inconsistency when using `IAmHonestlyDuck` with the
|
||||
`interactWithCrocodile()` function---the composed `crocodile` must be
|
||||
explicitly named during the call:
|
||||
|
||||
```kotlin
|
||||
interactWithCrocodile(honestlyDuck.crocodile)
|
||||
```
|
||||
|
||||
Modify the solution to that exercise using class delegation so you can call a
|
||||
function `interactWithCrocodile(honestlyDuck)`. Make the `crocodile` argument to
|
||||
`IAmHonestlyDuck` `private` so you *cannot* expose `field`.
|
||||
function `interactWithCrocodile(honestlyDuck)`. Make the `IAmHonestlyDuck`
|
||||
`crocodile` parameter `private` so you *cannot* expose `field`.
|
|
@ -1,10 +1,11 @@
|
|||
## Class Delegation (#3)
|
||||
|
||||
What happens when you delegate to two interfaces that have one or more
|
||||
members in common? The started code contains two interfaces `A` and `B`
|
||||
that both have `foo()` member function.
|
||||
members in common? The starter code contains two interfaces `A` and `B`
|
||||
that both have a `foo()` member function.
|
||||
|
||||
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.
|
||||
Create a class `AImpl` that implements `A` and traces `"A.foo()"` for `foo()`
|
||||
and `"A.bar()"` for `bar()`. Create a similar implementation `BImpl` that
|
||||
implements `B`. Now create a class `Delegation` that delegates to both `A` and
|
||||
`B`. IntelliJ or the compiler will guide you in resolving the resulting
|
||||
issues.
|
|
@ -5,4 +5,4 @@ assume they're part of the third-party library). Implement the `mimicDuck()`
|
|||
function that dynamically adapts an object, accepting a `Crocodile` and
|
||||
returning an `IAmHonestlyDuck`. `IAmHonestlyDuck` should implement `Duck` and
|
||||
delegate both `Duck` member functions to `crocodile.bite()`. Is it possible to
|
||||
use the inheritance approach, or are you forced to use composition?
|
||||
use inheritance, or are you forced to use composition?
|
|
@ -12,8 +12,8 @@ fun main() {
|
|||
capture {
|
||||
testFortyTwo2(43)
|
||||
} contains
|
||||
listOf("expected:", "<43> " +
|
||||
"but was:", "<42>")
|
||||
listOf("expected:",
|
||||
"<43> but was:", "<42>")
|
||||
assertFails { testFortyTwo2(43) }
|
||||
capture {
|
||||
assertFails { testFortyTwo2() }
|
||||
|
|
Loading…
Reference in New Issue