|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | +# Incorrecto |
| 4 | +class Vehicle: |
| 5 | + def drive(self): |
| 6 | + raise NotImplementedError |
| 7 | + |
| 8 | + def play_music(self): |
| 9 | + raise NotImplementedError |
| 10 | + |
| 11 | + def open_trunk(self): |
| 12 | + raise NotImplementedError |
| 13 | + |
| 14 | +class Car(Vehicle): |
| 15 | + def drive(self): |
| 16 | + print("Driving a car") |
| 17 | + |
| 18 | + def play_music(self): |
| 19 | + print("Playing music in the car") |
| 20 | + |
| 21 | + def open_trunk(self): |
| 22 | + print("Opening the car trunk") |
| 23 | + |
| 24 | +class Bicycle(Vehicle): |
| 25 | + def drive(self): |
| 26 | + print("Riding a bicycle") |
| 27 | + |
| 28 | + def play_music(self): |
| 29 | + pass |
| 30 | + |
| 31 | + def open_trunk(self): |
| 32 | + pass |
| 33 | + |
| 34 | +# Correcto |
| 35 | +class Drivable(ABC): |
| 36 | + @abstractmethod |
| 37 | + def drive(self): |
| 38 | + pass |
| 39 | + |
| 40 | +class MusicPlayable(ABC): |
| 41 | + @abstractmethod |
| 42 | + def play_music(self): |
| 43 | + pass |
| 44 | + |
| 45 | +class TrunkOpenable(ABC): |
| 46 | + @abstractmethod |
| 47 | + def open_trunk(self): |
| 48 | + pass |
| 49 | + |
| 50 | +class Car(Drivable, MusicPlayable, TrunkOpenable): |
| 51 | + def drive(self): |
| 52 | + print("Driving a car") |
| 53 | + |
| 54 | + def play_music(self): |
| 55 | + print("Playing music in the car") |
| 56 | + |
| 57 | + def open_trunk(self): |
| 58 | + print("Opening the car trunk") |
| 59 | + |
| 60 | +class Bicycle(Drivable): |
| 61 | + def drive(self): |
| 62 | + print("Riding a bicycle") |
| 63 | + |
| 64 | + |
| 65 | +car = Car() |
| 66 | +car.drive() |
| 67 | +car.play_music() |
| 68 | +car.open_trunk() |
| 69 | + |
| 70 | +bicycle = Bicycle() |
| 71 | +bicycle.drive() |
| 72 | + |
| 73 | +### Ejercicio Extra ### |
| 74 | +class Printer(ABC): |
| 75 | + @abstractmethod |
| 76 | + def print(self, content: str): |
| 77 | + pass |
| 78 | + |
| 79 | +class ColorPrinter(ABC): |
| 80 | + @abstractmethod |
| 81 | + def print_color(self, content: str): |
| 82 | + pass |
| 83 | + |
| 84 | +class Scanner(ABC): |
| 85 | + @abstractmethod |
| 86 | + def scan(self, content: str): |
| 87 | + pass |
| 88 | + |
| 89 | +class Fax(ABC): |
| 90 | + @abstractmethod |
| 91 | + def send_fax(self, content: str): |
| 92 | + pass |
| 93 | + |
| 94 | +class BlackAndWhitePrinter(Printer): |
| 95 | + def print(self, content: str): |
| 96 | + print(f"Printing in black and white: {content}") |
| 97 | + |
| 98 | +class ColorPrinterOnly(ColorPrinter): |
| 99 | + def print_color(self, content: str): |
| 100 | + print(f"Printing in color: {content}") |
| 101 | + |
| 102 | +class MultifunctionPrinter(Printer, ColorPrinter, Scanner, Fax): |
| 103 | + def print(self, content: str): |
| 104 | + print(f"Printing in black and white: {content}") |
| 105 | + |
| 106 | + def print_color(self, content: str): |
| 107 | + print(f"Printing in color: {content}") |
| 108 | + |
| 109 | + def scan(self, content: str): |
| 110 | + print(f"Scanning: {content}") |
| 111 | + |
| 112 | + def send_fax(self, content: str): |
| 113 | + print(f"Sending fax: {content}") |
| 114 | + |
| 115 | + |
| 116 | +bw_printer = BlackAndWhitePrinter() |
| 117 | +bw_printer.print("Thesis") |
| 118 | + |
| 119 | +color_printer = ColorPrinterOnly() |
| 120 | +color_printer.print_color("Images") |
| 121 | + |
| 122 | +multifunction_printer = MultifunctionPrinter() |
| 123 | +multifunction_printer.print("Thesis") |
| 124 | +multifunction_printer.print_color("Images") |
| 125 | +multifunction_printer.scan("Credential") |
| 126 | +multifunction_printer.send_fax("Report") |
0 commit comments