我剛剛開始學習面向對象的Python,所以我有這個問題。爲什麼當我執行ChristmassTree()時會得到兩個字符串,但是當我執行時,有一個字符串= ChristmassTree()
首先,我已經創建了這些類:
class Tree:
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Tree.count += 1
def displayTree(self):
print "Total amount of Trees: %d"% Tree.count
class ChristmasTree(Tree):
def __init__(self):
Tree.count += 1
print "I am a ChristmasTree. I am the most beautiful among %d"% Tree.count + " trees"
所以,我不明白爲什麼我看到兩個字符串時,我執行以下命令:
ChristmasTree()
輸出:
I am a ChristmasTree. I am the most beautiful among {variable} trees
<Tree.ChristmasTree instance at 0x00000000002209788>
但我得到一行,當我執行此命令:
ct = ChristmasTree()
輸出:
I am a ChristmasTree. I am the most beautiful among {variable} trees
按照我的邏輯應該是這樣一行輸出:
I am a ChristmasTree. I am the most beautiful among {variable} trees
我在哪裏錯了?
謝謝,這只是爲了學習的目的。 我正在編輯代碼並試圖理解輸出。 – user2467011