-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest34_classinheritance.py
More file actions
78 lines (60 loc) · 2.12 KB
/
test34_classinheritance.py
File metadata and controls
78 lines (60 loc) · 2.12 KB
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
class Employee:
raise_amount = 1.04 # Class variable
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 f'{self.first} {self.last}'
def apply_raise(self):
self.pay = int(self.pay * self.raise_amount)
class Developer(Employee):
raise_amount = 1.10
def __init__(self, first, last, pay, prog_lang):
super().__init__(first, last, pay) # this calls the parent class
# or
# Employee.__init__(self, first, last, pay) # this is more for multiple inheritance
self.prog_lang = prog_lang
class Manager(Employee):
def __init__(self, first, last, pay, employee_list=None): # this constructor has a default parameter
super().__init__(first, last, pay) # this calls the parent class
# or
# Employee.__init__(self, first, last, pay) # this is more for multiple inheritance
if employee_list is None:
self.employees = []
else:
self.employees = employee_list
def add_emp(self, emp):
if emp not in self.employees:
self.employees.append(emp)
def remove_emp(self, emp):
if emp in self.employees:
self.employees.remove(emp)
def print_emp(self):
for emp in self.employees:
print(f'--> {emp.fullname()}')
dev_1 = Developer('me', 'maw', 20000, 'Python')
dev_2 = Developer('test2', 'noway', 30000, 'C/C++')
# print(help(Developer)) shows all the info about Developer class
print(dev_1.email)
print(dev_2.email)
print(dev_2.prog_lang)
print(dev_1.pay)
dev_1.apply_raise()
print(dev_1.pay)
mgr_1 = Manager('Sue', 'Smith', 90000, [dev_1])
print(mgr_1.email)
mgr_1.print_emp()
print()
mgr_1.add_emp(dev_2)
mgr_1.print_emp()
print()
mgr_1.remove_emp(dev_1)
mgr_1.print_emp()
print(isinstance(mgr_1, Employee))
print(isinstance(mgr_1, Manager))
print(isinstance(mgr_1, Developer))
print(issubclass(Developer, Employee))
print(issubclass(Manager, Employee))
print(issubclass(Manager, Developer))