python创建类的注意事项
2018-07-03 本文已影响0人
在做算法的巨巨
class Employee:
'所有员工的基类'
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)
创建实例时,报错:object takes no parameters
查找原因发现,init前后应该是分别两个“下划线”
修改代码应该是,
class Employee:
'所有员工的基类'
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)