Using inheritance in python

后端 未结 4 1242
轮回少年
轮回少年 2021-01-03 06:36

this is my homework assignment, I saw it posted on the website before, but it looks like it was unsolved and I got a different error message than a person asking that questi

4条回答
  •  悲&欢浪女
    2021-01-03 07:39

    The error is pretty clear. salary is not defined in the __init__ method of Executive.

    You used wage as an argument to __init__, but salary when calling __init__ of your parent class, so you should stick to one variable name:

    class Executive(Employee):
        def __init__(self, name, salary, yearlyBonus):
            Employee.__init__(self, name, salary) 
    

    Also, you can get around typing all of those parameters in each time by using *args:

    class Executive(Employee):
        def __init__(self, *args, yearlyBonus):
            super(Executive, self).__init__(*args)
    

    Use super() instead of calling the parent class's __init__ method. It makes multiple inheritance a bit easier.

提交回复
热议问题