Python类的未绑定错误

Python类的未绑定错误,python,class,static,Python,Class,Static,我到处都在研究,虽然我发现了相同的概念,但我似乎无法找到我错误的答案 我之前没有发布,因为我的帐户信息被遗忘在堆栈上,但我对这个初学者的错误感到非常失望 class Person(object): def __init__(self, name, phone): self.name = name self.phone = phone class Employee(Person): total_salary = 0 @staticme

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

我之前没有发布,因为我的帐户信息被遗忘在堆栈上,但我对这个初学者的错误感到非常失望

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())

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

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)
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())
对于这种特定情况,您需要总工资,即所有员工工资的总和。你需要一批员工

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())

显然,您需要做更多的调整,如员工和学生之间的排序等。但希望这足以让您开始。

您的总体设计存在问题,这些问题已在其他答案中介绍过,因此我将尝试解释当前代码不起作用的原因:

  • 您已经创建了一个名为
    total\u salary
    的类属性,然后使用相同名称的方法对其进行阴影处理
  • 当需要使用类方法时,您使用的是静态方法
  • 您需要调用
    total\u salary
    作为一个方法,以返回其值
  • 这些问题可以这样解决:

    leslie = Professor("Leslie", "985-2363", 50000.00, "Info 501")
    print "Leslie's Salary: " + str(leslie.salary)
    
    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())
    

    但是请注意,代码仍然会引发一个
    属性错误
    ,因为您试图调用尚未定义的
    Student.mean\u gpa

    您的类员工有一个同名的属性和方法。调用Employee.total_salary将返回该函数,因为在
    MRO
    中它将替换该属性

    更改:

    class Employee(Person):
        total_salary = 0
    

    您在
    Employee
    中的
    staticmethod total\u salary
    返回的变量不存在(因为它只在方法范围内查看),您应该返回Employee.total\u salary\u值。像这样:

    @staticmethod
    def total_salary():
        return Employee.total_salary_value
    
    员工
    初始
    中,您没有实例化您要继承的
    人员
    (与
    学生
    (继承
    人员
    )、
    教授
    员工
    (两者都继承
    员工
    )中的情况相同

    使用以下任一选项:

    Person.__init__(self,name,phone)
    

    仍然在init中,您没有将工资添加到
    total\u salary\u值中

    添加以下内容:

    Employee.total_salary_value += self.salary
    
    另外,在
    Student
    中不需要使用
    self.name
    self.phone
    ,因为
    Person
    已经具有此属性。(该属性和
    Professor
    Staff
    中的其他变量相同)

    最后,
    Student
    没有名为
    mean\u gpa
    的方法。你必须实现它

    代码如下:

    class Person(object):
        def __init__(self, name, phone):
            self.name = name
            self.phone = phone
    
    class Employee(Person):
        total_salary_value = 0
    
        @classmethod
        def total_salary(cls):
            return cls.total_salary_value
    
        def __init__(self, name, phone, salary):
            Person.__init__(self,name,phone)
            self.salary = salary
            Employee.total_salary_value += self.salary
    
    class Student(Person):
        all_gpa = []
    
        def __init__(self, name, phone, gpa):
            Person.__init__(self,name,phone)
            self.gpa = gpa
            Student.all_gpa.append(self.gpa)
    
        def __str__(self):
            return "Person {0} has phone {1} and is a Student with gpa {2}".format(self.name,self.phone,self.gpa)
    
        @classmethod
        def mean_gpa(cls):
            return sum(cls.all_gpa)/len(cls.all_gpa)
    
    
    class Professor(Employee):
        def __init__(self, name, phone, salary, clas_teach):
            Employee.__init__(self,name,phone,salary)
            self.clas_teach = clas_teach
    
        def __str__(self):
            return "Person {0} has phone {1} and is an Employee with salary {2}\n" \
                   "and is a Professor assigned to class {3}".format(self.name,self.phone,self.salary,self.clas_teach)
    
    
    class Staff(Employee):
        def __init__(self, name, phone, salary, position):
            Employee.__init__(self, name, phone, salary)
            self.position = position
    
        def __str__(self):
            return "Person {0} has phone {1} and is an Employee with salary {2}\n" \
                   "and is Staff with title {3}".format(self.name, self.phone, self.salary, self.position)
    
    # 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())
    
    编辑:

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


    另外,您的
    \uuuu str\uuuu
    可以使用.format()更清楚地显示出来

    错误现在是,但是如果没有@staticmethod,它给了我你永远不会在total_salary方法中求和工资,你传递了两个没有使用的参数,
    total_salary
    是一个类属性,除了返回之外,它也没有被使用。你想做什么?你想如何解决这个问题?我正在尝试一个在People中添加员工的工资,但是还要确保如果我添加一个员工,它会添加工资。你不认为你需要另一个类吗,比如,
    大学
    ,它有一个名为:
    ,这是一个人员列表的属性,然后你就有了
    人员总预算
    方法h遍历所有人员,如果他们是员工,则添加工资。
    @staticmethod
    和函数定义中的
    self
    很可能是错误的。