2016-12-01 45 views
1

我剛剛開始學習面向對象的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 

我在哪裏錯了?

回答

0

我沒有得到你想要在這裏做什麼。但至少,您需要在ChristmasTree__init__方法中撥打Tree.__init__(name,age)

+0

謝謝,這只是爲了學習的目的。 我正在編輯代碼並試圖理解輸出。 – user2467011

相關問題