Skip to content

Commit b2db09b

Browse files
authored
Road mouredev#29 - Kotlin
1 parent b3cef7a commit b2db09b

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
fun main() {
2+
//Uso Incorrecto
3+
abstract class Aveh {
4+
abstract fun walk(): String
5+
abstract fun fly(): String
6+
}
7+
class Bird_: Aveh() {
8+
override fun walk() = "Estoy andando"
9+
override fun fly() = "Estoy volando"
10+
}
11+
class Penguin_: Aveh() {
12+
override fun walk() = "Estoy andando"
13+
override fun fly() = "Estoy volando"
14+
}
15+
val bird_ = Bird_()
16+
println(bird_.walk())
17+
val penguin_ = Penguin_()
18+
println(penguin_.fly())
19+
/*
20+
Es incorrecto porque los pingünos no vuelan. Para que se de este principio tienen que venir de una clase
21+
con métodos generales y cada subclase sus métodos específicos
22+
*/
23+
24+
//Uso correcto
25+
abstract class Ave {
26+
abstract fun walk(): String
27+
}
28+
class Bird: Ave() {
29+
override fun walk() = "Estoy andando"
30+
fun fly() = "Estoy volando"
31+
}
32+
class Penguin: Ave() {
33+
override fun walk() = "Estoy andando"
34+
fun swim() = "Estoy nadando"
35+
}
36+
val bird = Bird()
37+
println(bird.fly())
38+
val penguin = Penguin()
39+
println(penguin.swim())
40+
/*
41+
Así es correcto porque la interfaz tiene los métodos generales y ya las subclases hacen lo que
42+
sea específico de éstas más lo que herede.
43+
*/
44+
45+
println("\n${"*".repeat(7)} EJERCICIO EXTRA ${"*".repeat(7)}")
46+
class PrinterByW: Printer {
47+
override fun print(doc: String): String {
48+
return "Ha elegido impresión en blanco y negro. Imprimiendo '$doc'"
49+
}
50+
}
51+
class PrinterColor: Printer {
52+
override fun print(doc: String): String {
53+
return "Ha elegido impresión a color. Imprimiendo '$doc'"
54+
}
55+
}
56+
class MultiPrinter: PrinterFunctions {
57+
override fun print(doc: String): String {
58+
return "Imprimiendo $doc"
59+
}
60+
61+
//scan y sendFax están definidas en la interface y si no van a tener
62+
//modificaciones, no es necesarios sobreescribirlas
63+
}
64+
65+
val doc1 = PrinterByW()
66+
println(doc1.print("El Quijote"))
67+
val doc2 = PrinterColor()
68+
println(doc2.print("La Celestina"))
69+
val doc3 = MultiPrinter()
70+
println(doc3.scan("Apuntes Matemáticas"))
71+
println(doc3.sendFax("Documento_Secreto", 321456780))
72+
73+
}
74+
75+
interface Printer {
76+
fun print(doc: String): String
77+
}
78+
79+
interface PrinterFunctions: Printer {
80+
fun scan(doc: String): String {
81+
return "Escaneando $doc"
82+
}
83+
fun sendFax(doc: String, fax: Int): String {
84+
return "Enviando $doc al fax $fax"
85+
}
86+
}

0 commit comments

Comments
 (0)