-2
class Employee:
'Common base class 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 would create first object of Employee class"
emp1 = Employee("Zara", 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount
如果我以後添加年齡屬性爲emp1.age = 7。那我該如何訪問它?如何訪問在後期添加的Python中的類屬性?
我試過 - 打印出「employee1的年齡:%d」%d self.age 但是它給的錯誤。
它應該工作,但檢查你的問題:縮進是可怕的,「其給予錯誤」對我們不是很有用。 –