-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_4.py
More file actions
106 lines (80 loc) · 3.07 KB
/
tutorial_4.py
File metadata and controls
106 lines (80 loc) · 3.07 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
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
class Employee(object):
raise_amount = 1.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)
class Developer(Employee):
raise_amount = 1.10
def __init__(self, first, last, pay, programming_language):
# super().__init__() will pass the same parameters to the parent class __init__ function
super().__init__(first, last, pay)
# the parent class name can also be used
# Employee.__init__(self, first, last, pay)
self.programming_language = programming_language
class Manager(Employee):
def __init__(self, first, last, pay, employees=None):
# Never pass mutable data type like list or dictionnary as an argument
super().__init__(first, last, pay)
if employees is None:
self.employees = []
else:
self.employees = employees
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("--> ", emp.fullname())
dev_1 = Employee("John", "Doe", 50000)
dev_2 = Developer("Steve", "Smith", 60000, "Python")
dev_3 = Developer("Jane", "Doe", 90000, "Java")
# Even without any customisation to the Developer class, it inherits all the methods and attributes from its parent class
print(dev_1.email)
print(dev_2.email)
print()
# print(help(Developer))
# print()
print("Employee Class")
print("{} : {}".format(dev_1.fullname(), dev_1.pay))
dev_1.apply_raise()
print("{} : {}".format(dev_1.fullname(), dev_1.pay))
print()
print("Developer Class")
print("{} : {}".format(dev_2.fullname(), dev_2.pay))
dev_2.apply_raise()
print("{} : {}".format(dev_2.fullname(), dev_2.pay))
print()
print("Inheritance")
print("{} : {} : {}".format(dev_2.fullname(), dev_2.pay, dev_2.programming_language))
print("{} : {} : {}".format(dev_3.fullname(), dev_3.pay, dev_3.programming_language))
print()
print("Manager Class")
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.add_emp(dev_3)
mgr_1.print_emp()
print()
mgr_1.remove_emp(dev_1)
mgr_1.print_emp()
print()
print("Check if an object is an instance of a class")
print("if mgr_1 is an instance of class Manager: ", isinstance(mgr_1, Manager))
print("if mgr_1 is an instance of class Employee: ", isinstance(mgr_1, Employee))
print("if mgr_1 is an instance of class Developer: ", isinstance(mgr_1, Developer))
print()
print("Check if a class is a subclass of an another class")
print("Is Developer a subclass of Employee: ", issubclass(Developer, Employee))
print("Is Manager a subclass of Employee: ", issubclass(Manager, Employee))
print("Is Developer a subclass of Manager: ", issubclass(Developer, Manager))