python learning

CODE:

class Employee:
    "所有員工的基類"
    empCount=0

    def __init__(self,name,salary):
        self.name=name
        self.salary=salary
        empCount+=1

ERROR:

 UnboundLocalError: local variable 'empCount' referenced before assignment

CORRECT:

 empCount+=1 改爲 Employee.empCount+=1

NOTE:

 class的attribute不能單獨存在,必須依附class,纔有class.attribute
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章