2012-11-08 152 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向父母反驳争论。 –