我是python的新手,並且遇到了讓代碼工作的問題。我一直在遇到同樣的問題。當我運行這個時,我收到錯誤信息:Python中的類 - TypeError:object()不帶參數
TypeError: object() takes no parameters.
我已經在下面給出了完整的錯誤信息。
這裏是我的代碼:
class Bird:
_type = ""
def bird(self, type):
self._type = type
def display(self):
print(self._type)
class Species:
_bird = None
_type = ""
def set_bird(self, bird):
self._bird = bird
def display(self):
print(self._type)
self._bird.display(self)
class Cardinal(Species):
def cardinal(self):
self._type = "Cardinal"
def main():
species = Cardinal()
species.set_bird(Bird("Red"))
species.display()
main()
您對如何聲明構造函數感到困惑。在python中,你使用'__init__'而不是類的名字。即使iy是那樣,爲什麼一個類'Bird'有一個構造函數'bird'? –
https://stackoverflow.com/questions/27078742/typeerror-object-takes-no-parameters – kta