[Python]Inheritance and super() method

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



# Add your code below!

class PartTimeEmployee(Employee):

    def calculate_wage(self,hours):

        self.hours = hours

        return hours * 12.00



    def full_time_wage(self,hours):

        return super(PartTimeEmployee,self).calculate_wage(hours)



milton = PartTimeEmployee("Howe")

print milton.full_time_wage(10)

 

 

 

 

 

So when I called super() I need to simplify the argument which is PartTimeEmployee and it called the its father Employee and send the argument self, so I can use the calculate_wage to print the result.

 

In fact in Python 3.0, the super() is much more clearer to use, just  super().calculate_wage(hours) is okay to be used.

 

 

 

//stackoverflow.com/questions/576169/understanding-python-super-with-init-methods

//stackoverflow.com/questions/1259547/is-it-a-good-idea-to-use-super-in-python

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据