-
Notifications
You must be signed in to change notification settings - Fork 0
/
14_Employee_Class__use_Overrided_method.py
63 lines (40 loc) · 1.81 KB
/
14_Employee_Class__use_Overrided_method.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
"""
sometimes you’ll be working with a derived class (or subclass) and realize that you’ve overwritten
a method or attribute defined in that class’ base class (also called a parent or superclass)
that you actually need. Have no fear! You can directly access the attributes or methods of a
superclass with Python’s built-in super call.
The syntax looks like this:
class Derived(Base):
def m(self):
return super(Derived, self).m()
Where m() is a method from the base class.
"""
class Employee(object):
"""Models real-life employees!"""
def __init__(self, employee_name):
self.employee_name = employee_name
def calculate_wage(self, hours):
self.hours = hours
return hours * 20.00
# class PartTimeEmployee inherits from Employee. PartTimeEmployee.calculate_wage overrides Employee.calculate_wage.
# If you want to calculate the full wage, there is another method full_time_wage,
# to be used if you want to use the parent metod calculate_wage
class PartTimeEmployee(Employee):
def full_time_wage(self, hours):
return super(PartTimeEmployee, self).calculate_wage(hours)
def calculate_wage(self, hours):
self.hours = hours
return hours * 12.00
Joel = PartTimeEmployee("Joel")
print Joel.calculate_wage(23) # 276
Christina = Employee("Christina")
print Christina.calculate_wage(23) # 460
milton = PartTimeEmployee("Milton")
print milton.full_time_wage(10) # 200
print milton.calculate_wage(10) # 120
# https://discuss.codecademy.com/t/what-does-it-mean-to-override-a-method/340795
# https://discuss.codecademy.com/t/how-do-i-print-my-new-employee-s-full-time-wage-results/340797
"""
By defining a method with the same name as found in the base class, Employee,
we are overriding the functionality so that any PartTimeEmployees will have their wages calculated appropriately.
"""