[英]Class constructor raises TypeError
class employees:
emp_count = 0
def _init_(self, name, age):
self.name = name
self.age = age
employees.emp_count += 1
def displaycounts(self):
print "total counts is %d" % employee.emp_count
def displaydetails(self):
print "Name :", self.name, ",age:", self.age
obj = employees("krishna",4000)
obj1 = employees("shashi",10000)
obj3 = employees
obj.displaydetails()
obj1.displaydetails()
obj3.displaycounts()
回溯(最近一次調用):文件“C:/Python27/dd”,第 14 行,在 obj = 員工(“krishna”,4000)類型錯誤:此構造函數不帶參數
def __init__(self, name, age):
就是你要找的。
此外,您必須縮進屬於函數的行,因此
def __init__(self, name, age):
self.name = name
self.age = age
employees.emp_count += 1
更像是……等等,但鑒於您看到的錯誤,我猜這是堆棧溢出格式問題。
這里還有一些其他問題,但這應該可以幫助您克服當前的障礙。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.