美文网首页
python创建类的注意事项

python创建类的注意事项

作者: 在做算法的巨巨 | 来源:发表于2018-07-03 20:11 被阅读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)
    

    相关文章

      网友评论

          本文标题:python创建类的注意事项

          本文链接:https://www.haomeiwen.com/subject/cobduftx.html