|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | +#Uso incorrecto |
| 4 | +class Aveh(ABC): |
| 5 | + @abstractmethod |
| 6 | + def walk(self): |
| 7 | + pass |
| 8 | + |
| 9 | + @abstractmethod |
| 10 | + def fly(self): |
| 11 | + pass |
| 12 | + |
| 13 | +class Bird_(Aveh): |
| 14 | + def walk(self): |
| 15 | + return "El pájaro está caminando" |
| 16 | + |
| 17 | + def fly(): |
| 18 | + return "El pájaro está volando" |
| 19 | + |
| 20 | +class Penguin_(Aveh): |
| 21 | + def walk(self): |
| 22 | + return "El pingüino está caminando" |
| 23 | + |
| 24 | + def fly(self): |
| 25 | + return "El pingüino está volando" |
| 26 | + |
| 27 | +bird_ = Bird_() |
| 28 | +print(bird_.walk()) |
| 29 | + |
| 30 | +''' |
| 31 | +Es incorrecto porque los pingünos no vuelan. Para que se de este principio tienen que venir de una clase |
| 32 | +con métodos generales y cada subclase sus métodos específicos |
| 33 | +''' |
| 34 | + |
| 35 | +#Uso correcto |
| 36 | +class Ave(ABC): |
| 37 | + @abstractmethod |
| 38 | + def walk(self): |
| 39 | + pass |
| 40 | + |
| 41 | +class Bird(Ave): |
| 42 | + def walk(self): |
| 43 | + return "El pájaro está caminando" |
| 44 | + |
| 45 | + def fly(self): |
| 46 | + return "El pájaro está volando" |
| 47 | + |
| 48 | +class Penguin(Ave): |
| 49 | + def walk(self): |
| 50 | + return "El pingüino está caminando" |
| 51 | + |
| 52 | + def swim(self): |
| 53 | + return "El pingüino está nadando" |
| 54 | + |
| 55 | +bird = Bird() |
| 56 | +print(bird.fly()) |
| 57 | +penguin = Penguin() |
| 58 | +print(penguin.swim()) |
| 59 | + |
| 60 | +print("\n", "*"*7, "EJERCICIO EXTRA", "*"*7) |
| 61 | +class Printer(ABC): |
| 62 | + @abstractmethod |
| 63 | + def printing(self, doc): |
| 64 | + return f"Imprimiendo {doc}" |
| 65 | + |
| 66 | +class Printer_Functions(Printer): |
| 67 | + @abstractmethod |
| 68 | + def scan(self, doc): |
| 69 | + return f"Escaneando \"{doc}\"" |
| 70 | + |
| 71 | + @abstractmethod |
| 72 | + def send_fax(self, doc, fax): |
| 73 | + return f"Enviando \"{doc}\" al {fax}" |
| 74 | + |
| 75 | + |
| 76 | +class ByW_Printer(Printer): |
| 77 | + def printing(self, doc): |
| 78 | + return f"Imprimiendo en blanco y negro: {doc}" |
| 79 | + |
| 80 | +class Color_Printer(Printer): |
| 81 | + def printing(self, doc): |
| 82 | + return f"Imprimiendo a color: {doc}" |
| 83 | + |
| 84 | +class Multi_Printer(Printer_Functions): |
| 85 | + def printing(self, doc): |
| 86 | + return f"Imprimiendo a color: {doc}" |
| 87 | + |
| 88 | + def scan(self, doc): |
| 89 | + return super().scan(doc) |
| 90 | + |
| 91 | + def send_fax(self, doc, fax): |
| 92 | + return super().send_fax(doc, fax) |
| 93 | + |
| 94 | +byn = ByW_Printer() |
| 95 | +print(byn.printing("El Quijote")) |
| 96 | + |
| 97 | +color = Color_Printer() |
| 98 | +print(color.printing("Batman Begins")) |
| 99 | + |
| 100 | +printer = Multi_Printer() |
| 101 | +print(printer.send_fax("Trabajo Final", 254789630)) |
0 commit comments