... | ... | @@ -3,6 +3,7 @@ |
|
|
- [**Cheatsheet**](https://koenig-media.raywenderlich.com/uploads/2018/08/RW_Kotlin_Cheatsheet_1_0.pdf)
|
|
|
- Lambdas
|
|
|
- Functions
|
|
|
- Objects
|
|
|
|
|
|
## Lambda, anonymná funkcia
|
|
|
|
... | ... | @@ -18,3 +19,111 @@ Alebo môžme použiť lambda výraz: |
|
|
val adder: (Int, Int) -> Int = { x, y -> x + y}
|
|
|
```
|
|
|
|
|
|
### Úloha 1
|
|
|
|
|
|
Vytvorte lambda výraz, ktorý vezme číslo a vráti jeho trojnásobok.
|
|
|
|
|
|
## Dedičnosť, _inheritance_
|
|
|
|
|
|
- [dokumentácia](https://kotlinlang.org/docs/reference/classes.html#inheritance)
|
|
|
- trieda vie dediť od inej triedy, tým pádom získa všetky metódy a atribúty
|
|
|
- (nie je to síce úplne pravda, závisí na viditeľnosti, ktorú budeme v tomto kurze ignorovať)
|
|
|
- aby sme mohli triedu podediť, musí mať kľúčové slovo `open`
|
|
|
|
|
|
![Screenshot_2019-03-16_at_19.00.16](uploads/19fac42e4bd1ee55eed0fa81b02bf84b/Screenshot_2019-03-16_at_19.00.16.png)
|
|
|
|
|
|
```kotlin
|
|
|
open class Animal {
|
|
|
var name: String = ""
|
|
|
override fun toString(): String {
|
|
|
return name
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Dog: Animal() {
|
|
|
override fun toString(): String {
|
|
|
return "Dog " + super.toString()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Cat: Animal() {
|
|
|
override fun toString(): String {
|
|
|
return "Cat " + super.toString()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
fun main(args: Array<String>) {
|
|
|
val dog = Dog()
|
|
|
val cat = Cat()
|
|
|
dog.name = "Alik"
|
|
|
cat.name = "Micka"
|
|
|
println(dog)
|
|
|
println(cat)
|
|
|
}
|
|
|
```
|
|
|
|
|
|
### Úloha 2
|
|
|
|
|
|
```kotlin
|
|
|
open class Point2D(var x: Int, var y: Int) {
|
|
|
fun move(otherPoint: Point2D) {
|
|
|
x += otherPoint.x
|
|
|
y += otherPoint.y
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
Vytvorte bod `Point3D`, ktorý bude dediť od `Point2D` a bude mať tretiu súradnicu _z_.
|
|
|
Jej konštruktor bude brať 3 parametre -- x, y, z.
|
|
|
|
|
|
Vytvorte metódu `move(Point3D)`, aby fungovala v 3D priestore a využite metódu `move(Point2D)`.
|
|
|
|
|
|
```kotlin
|
|
|
val p1 = Point3D(1, 4, 7)
|
|
|
val p2 = Point3D(3, 2, 1)
|
|
|
p1.move(p2)
|
|
|
println(p1.x) // 4
|
|
|
println(p1.y) // 6
|
|
|
println(p1.z) // 8
|
|
|
```
|
|
|
|
|
|
## Rozhranie, _interface_
|
|
|
|
|
|
- Rozhranie je trieda bez implementácie, obsahuje iba holé názvy metód.
|
|
|
- Triedy implementujú rozhranie, pričom musia implementovať všetky metódy.
|
|
|
- Tieto triedy sa potom môžu použiť na mieste, kde má byť použité rozhranie.
|
|
|
|
|
|
|
|
|
```kotlin
|
|
|
interface Countable {
|
|
|
fun count(): Int
|
|
|
}
|
|
|
|
|
|
class Three : Countable {
|
|
|
override fun count(): Int = 3
|
|
|
}
|
|
|
|
|
|
class Five : Countable {
|
|
|
override fun count(): Int = 5
|
|
|
}
|
|
|
class Printer {
|
|
|
fun printOnPaper(countable: Countable) {
|
|
|
println("The amount is " + countable.count())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
fun main(args: Array<String>) {
|
|
|
val printer = Printer()
|
|
|
printer.printOnPaper(Three())
|
|
|
printer.printOnPaper(Five())
|
|
|
}
|
|
|
```
|
|
|
|
|
|
> Všimnite si, že v predošlom príklade sme zbytočne vytvárali nový objekt `Printer`.
|
|
|
Stačí nám predsa iba jeden objekt, nad ktorým budeme volať metódy.
|
|
|
Jeden objekt vytvoríme nahradením kľúčového slova `class` za slovo `object`.
|
|
|
Metóda sa potom zavolá cez názov objektu, t.j. `Printer.printOnPaper()`.
|
|
|
|
|
|
### HW
|
|
|
|
|
|
TODO |
|
|
\ No newline at end of file |