好吧,我研究了很多帖子,但没有一个让我修复我的代码。
我正在学习python,目前正在学习面向对象的课程。
这是我的代码: -
class Employee:
'Common base calss for all employees'
empCount = 0
def __init__(self,name,salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name: ", self.name, ",Salary: ", self.salary
"This whould create first object of Employee class"
emp1 = Employee("Zara", 2000)
"This whould create second object of employee class"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount()
然后当我运行它时: -
Name: Zara ,Salary: 2000
Name: Manni ,Salary: 5000
Traceback (most recent call last):
File "D:/Python Programs/Object Oriented-Classes(Python 2).py", line 46, in
<module>
print "Total Employee %d" % Employee.empCount()
TypeError: 'int' object is not callable
答案 0 :(得分:0)
更改此
print "Total Employee %d" % Employee.empCount()
到这个
print "Total Employee %d" % Employee.empCount
答案 1 :(得分:0)
查看错误。
TypeError : 'int' object is not callable
您正在尝试调用类变量Employee.empCount
,这是一个整数,因此无法调用。
只需将其更改为Employee.empCount
即可打印出变量。
答案 2 :(得分:0)
您写了&#34; Employee.empCount()&#34;。记住,&#34;()&#34;这用于调用函数。 就像你可以做的那样&#34; Employee.displayCount()&#34;,因为displayCount()是一个函数。 但要访问变量,只需这样做:
print "Total Employee %d" % Employee.empCount