0

2つのクラスを作成し、それらをテストしようとしていますが、次のエラーが発生し、一生の間、何が問題なのかわかりません。

これらのクラスをモジュールとして使用し、ユーザーからの入力を取得して引数を設定するという考え方ですが、今のところ、クラスをテストしているだけです。

エラー

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 = 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 に答える
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 に答える