0

我创建了两个类,我正在尝试测试它们,但是我收到了以下错误,并且我一生都看不到有什么问题。

这个想法是将这些类用作一个模块,然后从用户那里获取输入以填充参数,但现在我只是在测试这些类。

错误

File "./Employees.py", line 38, in <module>
emp1Atr.displayAttributes()
AttributeError: Attribute instance has no attribute 'displayAttributes'

下面的代码

#!/usr/bin/python

class Employee:
    'Practice class'
    empCount = 0

    def __init__(self, salary):
            self.salary = salary
            Employee.empCount += 1
    def displayCount(self):
            print "Total Employees %d" % Employee.empCount

    def displayEmployee(self):
            print "Salary: ", self.salary


class Attribute(Employee):
    'Defines attributes for Employees'
    def __init__(self, Age, Name, Sex):

            def Age(self):
                    self.Age = Age

            def Name(self):
                    self.Name = Name

            def Sex(self):
                    self.Sex = Sex

            def displayAttributes(self):
                    print "Name: ", self.Name + "\nAge: ", self.Age + "\nSex: ", self.Sex


emp1Sal = Employee(2000)
emp1Atr = Attribute(12, "John", "man")

emp1Sal.displayEmployee()
emp1Atr.displayAttributes()
4

3 回答 3

0

如果那是正确的复制粘贴,那么您在 Attribute 类中的缩进太深了。之后的所有方法__init__(self)都缩进,就好像它们在内部的本地方法__init__(self)而不是对象中一样。因此,如果您正确地对它们进行缩进并正确设置属性age, name and sex__init__()它应该可以工作。

于 2012-11-08T23:32:35.140 回答
0

好的,我只是在不打电话给父母的情况下重试了它,它可以工作。你能解释一下你为什么添加那条线吗?

新代码

#!/usr/bin/python

class Employee:
    'Practice class'
    empCount = 0

    def __init__(self, salary):
            self.salary = salary
            Employee.empCount += 1
    def displayCount(self):
            print "Total Employees %d" % Employee.empCount

    def displayEmployee(self):
            print "Salary: ", self.salary


class Att(Employee):
    'Defines attributes for Employees'
    def __init__(self, Age, Name, Sex):
            self.Age = Age
            self.Name = Name
            self.Sex = Sex

    def display(self):
            print "Name: ", self.Name + "\nAge: ", self.Age,  "\nSex: ", self.Sex


emp1Atr = Att(12, "Da", "man")


emp1Atr.display()
于 2012-11-09T00:05:48.617 回答
0
#!/usr/bin/python
class Employee:

'Practice class'
empCount = 0

def __init__(self, salary = None): #made salary as an optional argument
        self.salary = salary
        Employee.empCount += 1
def displayCount(self):
        print "Total Employees %d" % Employee.empCount

def displayEmployee(self):
        print "Salary: ", self.salary


class Attribute(Employee):
    'Defines attributes for Employees'
    def __init__(self, Age, Name, Sex):
        Employee.__init__(self) #Calling the parent class init method
        #Assigning Attribute the values passed in the __init__ method

        self.Age = Age
        self.Name = Name
        self.Sex = Sex  
#Making a class method which is why it is intended out of the init method
#This was the reason you got the first error of display attributes
def displayAttributes(self):
print "Name: ", self.Name + "\nAge: ", self.Age , "\nSex: ", self.Sex #Changed the + to ',' between self.Age and "\nSex"


emp1Sal = Employee(2000)
emp1Atr = Attribute(12, "John", "man")

emp1Sal.displayEmployee()
emp1Atr.displayAttributes()
于 2012-11-08T23:38:07.887 回答