|
| 1 | +#Inheritance |
| 2 | + |
| 3 | +class Animal: |
| 4 | + def __init__(self, name): |
| 5 | + self.name = name |
| 6 | + |
| 7 | + def sound(self): |
| 8 | + return "This animal does not have a specific sound" |
| 9 | + |
| 10 | + def print(self): |
| 11 | + print(f"{self.name}: {self.sound()}") |
| 12 | + |
| 13 | + |
| 14 | +class Dog(Animal): |
| 15 | + def __init__(self, name, breed): |
| 16 | + super().__init__(name) |
| 17 | + self.breed = breed |
| 18 | + |
| 19 | + def print(self): |
| 20 | + print(f"{self.name} ({self.breed}): {self.sound()}") |
| 21 | + |
| 22 | + def sound(self): |
| 23 | + return "Woof" |
| 24 | + |
| 25 | + |
| 26 | +class Cat(Animal): |
| 27 | + def sound(self): |
| 28 | + return "Meow" |
| 29 | + |
| 30 | + |
| 31 | +def print_sound(animal: Animal): |
| 32 | + print(animal.sound()) |
| 33 | + |
| 34 | + |
| 35 | +# Create objects |
| 36 | +dog = Dog("Bobbi", "Beagle") |
| 37 | +cat = Cat("Kuba") |
| 38 | + |
| 39 | +# Print their sounds |
| 40 | +print_sound(dog) # Woof |
| 41 | +print_sound(cat) # Meow |
| 42 | + |
| 43 | +# Print full profile |
| 44 | +dog.print() # Bobbi: Woof |
| 45 | +cat.print() # Kuba: Meow |
| 46 | + |
| 47 | + |
| 48 | +#Extra Exercise |
| 49 | + |
| 50 | +class Employee: |
| 51 | + |
| 52 | + def __init__(self, id, name): |
| 53 | + self.id = id |
| 54 | + self.name = name |
| 55 | + self.employees = [] |
| 56 | + |
| 57 | + def add(self, employee): |
| 58 | + self.employees.append(employee) |
| 59 | + |
| 60 | + def print_employees(self): |
| 61 | + for employee in self.employees: |
| 62 | + print(employee.name) |
| 63 | + |
| 64 | +class Manager(Employee): |
| 65 | + def coordinate_projects(self): |
| 66 | + print(f"{self.name} is coordinating all the projects") |
| 67 | + |
| 68 | +class ProjectManager(Employee): |
| 69 | + def __init__(self, id, name, project): |
| 70 | + super().__init__(id, name) |
| 71 | + self.project = project |
| 72 | + |
| 73 | + def coord_project(self): |
| 74 | + print(f"{self.name} is coordinating it owns project") |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +class Programmer(Employee): |
| 79 | + def __init__(self, id, name, language): |
| 80 | + super().__init__(id, name) |
| 81 | + self.language = language |
| 82 | + |
| 83 | + def code(self): |
| 84 | + print(f"{self.name} is coding in {self.language}") |
| 85 | + |
| 86 | + def add(self, employee): |
| 87 | + print(f"A programmer doesn’t supervise any employees. {employee.name} will not be added.") |
| 88 | + |
| 89 | +my_manager = Manager(1, "Anna") |
| 90 | +my_project_manager = ProjectManager(2, "Pablo", "Project 1") |
| 91 | +my_project_manager2 = ProjectManager(3, "David", "Project 2") |
| 92 | +my_programmer = Programmer(4, "Rose", "Java") |
| 93 | +my_programmer2 = Programmer(5, "Ross", "JavaScript") |
| 94 | +my_programmer3 = Programmer(6, "Marta", "Python") |
| 95 | + |
| 96 | +my_manager.add(my_project_manager) |
| 97 | +my_manager.add(my_project_manager2) |
| 98 | +my_project_manager.add(my_programmer2) |
| 99 | +my_project_manager2.add(my_programmer3) |
| 100 | +my_project_manager.add(my_programmer) |
| 101 | + |
| 102 | +my_programmer3.add(my_programmer3) |
| 103 | + |
| 104 | + |
| 105 | +my_programmer.code() |
| 106 | +my_manager.coordinate_projects() |
| 107 | +my_project_manager.coord_project() |
| 108 | +my_manager.print_employees() |
| 109 | +my_project_manager.print_employees() |
| 110 | +my_project_manager2.print_employees() |
0 commit comments