未绑定错误Python类

时间:2014-12-15 22:05:31

标签: python class static

我到处研究过,虽然我发现了相同的概念,但我似乎无法找到错误的答案。

我之前没有发帖,因为我的帐号信息在堆栈上被遗忘了,但我对这个初学者的错误感到非常沮丧。

class Person(object):

    def __init__(self, name, phone):
        self.name = name
        self.phone = phone

class Employee(Person):

    total_salary = 0

    @staticmethod
    def total_salary(self, salary):
        return total_salary


    def __init__(self, name, phone, salary):
        self.name = name
        self.phone = phone
        self.salary = salary




class Student(Person):

    def __init__(self, name, phone, gpa):
        self.gpa = gpa
        self.name = name
        self.phone = phone

    def __str__(self):
        reply = ""
        reply = "Person " + self.name + " has phone " + self.phone + "\n" + " and is a Student with gpa " + str(self.gpa)
        return reply

class Professor(Employee):
    def __init__(self, name, phone, salary, clas_teach):
        self.clas_teach = clas_teach
        self.name = name
        self.phone = phone
        self.salary = salary

    def __str__(self):
        reply = ""
        reply = "Person " + self.name + " has phone " + self.phone + "\n" + " and is an Employee with salary " + str(self.salary) + "\n"
        reply += " and is a Professor assigned to class " + self.clas_teach
        return reply

class Staff(Employee):
    def __init__(self, name, phone, salary, position):
        self.position = position
        self.name = name
        self.phone = phone
        self.salary = salary
    def __str__(self):
        reply = ""
        reply = "Person " + self.name + " has phone " + self.phone + "\n" + " and is an Employee with salary " + str(self.salary) + "\n"
        reply += " and is Staff with title " + self.position
        return reply
# Create a list of people
People = [ Student("Sandy", "326-8324", 3.65), Student("Jordan", "632-7434", 3.1), \
           Professor("Leslie", "985-2363", 50000.00, "Info 501"),  \
           Staff("Alex", "743-4638", 25000.00, "Editor") ]

# display information about our people
print "These are the people in the university:"
for person in People:
   print person

# display the total salaries of all our employees and average GPA
#  of all of our students
print
print "Our total university payroll budget is: " + str(Employee.total_salary)
print "Our average student GPA is: " + str(Student.mean_gpa())

3 个答案:

答案 0 :(得分:2)

您的主要误解是课程如何运作。在您的代码中,您正在调用类而不是类的实例:

print "Our total university payroll budget is: " + str(Employee.total_salary)
print "Our average student GPA is: " + str(Student.mean_gpa())

这里的关键是:

Employee.total_salary

相反,你应该做这样的事情:

leslie = Professor("Leslie", "985-2363", 50000.00, "Info 501")
print "Leslie's Salary: " + str(leslie.salary)

对于这种特定情况,您需要总工资单,即所有员工工资的总和。你需要在某个地方收集员工。

def University():
    def __init__(self):
        self.employees[]

    def add_employee(self, employee):
        self.employees.append(employee)

    def get_total_payroll(self):
        total = 0
        for employee in self.employees:
            total += employee.salary
        return total

然后使用该类的实例:

university = University()
university.add_employee(Professor("Leslie", "985-2363", 50000.00, "Info 501"))
university.add_employee(Staff("Alex", "743-4638", 25000.00, "Editor"))

print "Total payroll: " + str(university.get_total_payroll())

显然,您需要做出更多调整,例如在员工和学生之间进行排序等。但希望这足以让您入门。

答案 1 :(得分:0)

您的整体设计存在问题已在其他答案中介绍,因此我将尝试解释您当前代码无效的原因:

  1. 您已创建名为total_salary的类属性,然后使用相同名称的方法对其进行遮蔽。
  2. 当您需要使用类方法时,您正在使用静态方法。
  3. 您需要调用total_salary作为方法才能返回其值。
  4. 这些问题可以这样解决:

    class Employee(Person):
        _total_salary = 0
    
        @classmethod
        def total_salary(cls):
            return cls._total_salary
    ...
    
    print "Our total university payroll budget is: " + str(Employee.total_salary())
    

    但请注意,代码仍然会引发AttributeError,因为您正在尝试调用尚未定义的Student.mean_gpa

答案 2 :(得分:0)

您的类Employee具有属性AND具有相同名称的方法。调用Employee.total_salary将返回该函数,因为在MRO中它正在替换属性。

变化:

class Employee(Person):
    total_salary = 0

class Employee(Person):
    total_salary_value = 0

staticmethod total_salary中的Employee正在返回一个不存在的变量(因为它只在方法范围内查找),您应该返回Employee.total_salary_value。像这样:

@staticmethod
def total_salary():
    return Employee.total_salary_value

__init__的{​​{1}}中,您没有实例化您继承的EmployeePerson中的相同内容(继承Student),{ {1}}和Person(都继承Professor

使用:

Staff

Employee

仍然在init中,您没有将薪水添加到Person.__init__(self,name,phone)

添加:

super(Employee, self).__init__(name, phone) 

此外,您无需在total_salary_value中使用Employee.total_salary_value += self.salary self.name,因为self.phone已具有此属性。 (StudentPerson

中的变量和其他变量相同

最后,Professor没有名为Staff的方法。你必须实现它。

以下是代码:

Student

编辑:

正如@ekhumoro所说,最好使用@classmethod来返回值。

另一方面,使用.format()

可以使您的mean_gpa更加清晰