1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| # Developer is a sub class of Employee class Employee: raise_amount = 2.04
def __init__(self, first, last, pay): self.first = first self.last = last self.pay = pay self.email = first + '.' + last + '@company.com'
def fullname(self): return '{} {}'.format(self.first, self.last)
def apply_raise(self): self.pay = int(self.pay * self.raise_amount)
def set_regular_raise_amount(self, amount): self.raise_amount = amount
@classmethod def set_class_raise_amount(cls, amount): cls.raise_amount = amount
@staticmethod def is_workday(day): if day.weekday() == 5 or day.weekday() == 6: return False return True
class Developer(Employee): pass
emp_1 = Developer('Bob', 'Gobdo', 20) emp_2 = Developer('John', 'Youhs', 30)
print(emp_1.email) # Bob.Gobdo@company.com print(emp_2.email) # John.Youhs@company.com
print(help(Developer))
# change variable in sub class class Developer(Employee): raise_amount = 3
emp_1 = Employee('Bob', 'Gobdo', 20)
print(emp_1.pay) # 20 emp_1.apply_raise() print(emp_1.pay) # 40
dev_1 = Developer('Bob', 'Gobdo', 20)
print(emp_1.pay) # 20 emp_1.apply_raise() print(emp_1.pay) # 60
# inherit __init__ class Developer(Employee): raise_amount = 3
def __init__(self, first, last, pay, prog_lang): super().__init__(first, last, pay) self.prog_lang = prog_lang
dev_1 = Developer('Bob', 'Gobdo', 20, 'Python') print(dev_1.email) # Bob.Gobdo@company.com print(dev_1.prog_lang) # Python
# another sub class class Developer(Employee): raise_amount = 3
def __init__(self, first, last, pay, prog_lang): super().__init__(first, last, pay) self.prog_lang = prog_lang
class Manager(Employee):
def __init__(self, first, last, pay, employees = None): super().__init__(first, last, pay) if employees is None: self.employees = [] else: self.employees = employees
def add_employee(self, emp): if emp not in self.employees: self.employees.append(emp)
def remove_employee(self, emp): if emp in self.employees: self.employees.remove(emp)
def print_emps(self): for emp in self.employees: print('-->', emp.fullname())
dev_1 = Developer('Bob', 'Gobdo', 20, 'Python')
mgr_1 = Manager('Sue', 'Smith', 50, [dev_1]) print(mgr_1.email) # Sue.Smith@company.com mgr_1.print_emps() # --> Bob Gobdo
dev_2 = Developer('John', 'Youhs', 30, 'Java') mgr_1.add_employee(dev_2) mgr_1.print_emps() # --> Bob Gobdo # --> John Youhs
mgr_1.remove_employee(dev_2) mgr_1.print_emps() # --> Bob Gobdo
# isinstance, issubclass
print(isinstance(mgr_1, Manager)) # True print(isinstance(mgr_1, Employee)) # True print(isinstance(mgr_1, Developer)) # False
print(issubclass(Developer, Employee)) # True print(issubclass(Manager, Employee)) # True print(issubclass(Manager, Developer)) # False
|