2012-11-08 249 views
0

我創建了兩個類,我試圖測試它們,但我得到以下錯誤,並且我的生活中看不到錯誤。Python,類和繼承測試

這個想法是使用這些類作爲模塊,然後從用戶抓取輸入來填充參數,但現在我只是測試類。

錯誤

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

回答

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

我沒有看到差異對不起 – k3eper

+0

請看看。我已經更新了整個事情 –

+0

感謝您的迴應,請你解釋你做了什麼,我只是在學習幾天,所以即時消息:( – k3eper

0

如果這是正確的複製粘貼,那麼你在屬性類壓痕是一個太深。 __init__(self)之後的所有方法都縮進,就好像它們在__init__(self)之內的本地方法而不是該對象一樣。因此,如果你正確地超越它們並正確設置的屬性__init__(),它應該可以工作。

+0

我也認爲這是遠遠縮進,但我得到一個錯誤,如果我不縮進它 – k3eper

+0

文件「./Employees.py」,第21行 高清時代(個體經營) : ^ IndentationError:預計一個縮進塊 – k3eper

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

好吧,所以當定義__init__參數它必須在同一個實例,它不能在一個不同的def?感覺doh! – k3eper

+0

那麼它不是必需的,但只是一個很好的做法,所以你可以pr向父母反駁爭論。 –