在python中使用继承 [英] Using inheritance in python

查看:135
本文介绍了在python中使用继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的家庭作业,我看到它在网站上发布,但它看起来像它是未解决,我有一个不同的错误消息,比一个人提出这个问题。

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 question before.

问题的第一部分是定义继承自Employee的子类Worker,并且包含引用另一个员工的属性,该员工是员工的经理。您应该定义一个返回工人经理的方法get_manager。

the first part of the problem is to define the subclass Worker that inherits from Employee and includes an attribute that refers to another employee who is the worker's manager. You should define a method get_manager that returns the workers' manager.

示例:

worker = Worker("Fred", 52000, myboss)

问题是定义继承Employee并包含引用每年奖金的属性的子类执行。

The second part of the problem is to define the subclass Executive that inherits from Employee and includes an attribute that refers to the yearly bonus.

您应该覆盖工资方法,以根据他的/她的薪水和奖金。您应该在行政类的工资方法定义中使用Employee的工资方法。

You should override the wage method to compute executive pay based on his/her salary and bonus. You should use the wage method of Employee in the definition of the wage method for the Executive class.

示例:

executive = Executive("Kerry", 520000, 1040000)

我的代码写在下面,我得到的错误消息是:'全局名称'salary'没有定义'在行'Employee。 init (self,name,salary) (它适用于Worker类)。为什么会出现此错误,如何解决?

My code is written below and the error message I get is: 'global name 'salary' is not defined' in the line 'Employee.init(self, name, salary) ' for class Executive (It works for Worker class). Why do I get that error and how can I fix it?

感谢您的帮助!

class Employee(object):
    def __init__(self, name, salary):
        self._name = name
        self._salary = salary

    def my_name(self):
        return self._name

    def wage(self):
        return self._salary/26   # fortnight pay

class Worker(Employee):
    def __init__(self, name, salary, manager):
        Employee.__init__(self, name, salary)
        self._manager = manager

    def getManager(self):
        return self._manager

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

    def wage(self):
        return Employee.wage(self)


推荐答案

错误很清楚。在 Executive __ init __ 方法中未定义 salary

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

您使用 wage 作为 __ init __ 的参数,但调用父类的 __ init __ salary ,因此您应该坚持使用一个变量名:

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) 

此外,你可以通过使用 * args

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)


b $ b

使用 super()而不是调用父类的 __ init __ 方法。它使得多重继承更容易一些。

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

这篇关于在python中使用继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆