forked from kiat/Elements-of-Software-Design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_003_oop_method_overwriting.py
85 lines (63 loc) · 2.45 KB
/
example_003_oop_method_overwriting.py
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
"""
A Demonstration Example of Multiple Inheritance in Python.
"""
class Driver:
"""A Class to present Objects of Type Driver."""
def __init__(self, name, salary):
"""A Driver has a name, and salary."""
self.name = name
self.salary = float(salary)
def __str__(self):
"""A simple string method to convert a driver object to a string."""
return "I am " + self.name + " with a salary of " + str(self.salary)
def work_per_day(self, salary_per_hour):
"""
Calculates the number of hours a driver has
to work for a given amount of salary per month.
As an argument salary per hour is given.
"""
return round(self.salary / (30 * salary_per_hour), 2)
class Student:
"""A Student Class."""
def __init__(self, name, courses):
"""Students have a name and number of courses taken."""
self.name = name
self.courses = courses
def __str__(self):
"""A String representation of this object."""
return "I am " + self.name + " with a number of courses " + str(self.courses)
def work_per_day(self):
"""
Calculates the hours of study for
a student given the number of courses.
"""
return self.courses * 2
class StudentDriver(Driver, Student):
"""
A Student Driver is a person who is a student
and has to work as a driver part-time.
"""
def __init__(self, name, salary, courses):
"""A Student driver is a student and is also a Driver."""
super().__init__(name, salary)
Student.__init__(self, name, courses)
def __str__(self):
"""A string representation."""
return "I am " + self.name + " with a number of courses " \
+ str(self.courses) + " , and with a salary of " + str(self.salary)
def work_per_day(self, salary_per_hour=15):
"""Calculates hours of work for a student driver."""
return super().work_per_day(salary_per_hour) + Student.work_per_day(self)
def main():
"""
A main function to create objects of the above classes,
and demonstrate their method calls.
"""
# The method resolution order (or MRO) tells Python how to search for inherited methods
# print(StudentDriver.__mro__)
chris = StudentDriver("Chris", 2000, 4)
print(chris)
print("Chris works " + str(round(chris.work_per_day(), 2)) +
" hours each single day.")
if __name__ == "__main__":
main()