我是Python中的新手,如果問題對您來說非常簡單,請耐心等待。無法在方法重載中輸出子類變量
有人可以解釋爲什麼Dog類中的類變量,名稱在以下示例中導致錯誤?對於我來說d.name
可以被調用是沒有意義的,但d.eat()對於方法重載是不好的。非常感謝您的幫助!
class Animal: # parent class
name = 'Animal'
def eat(self):
print "Animal eating"
class Dog(Animal): # child class
name = 'Dog'
def eat(self):
print name
d = Dog()
print d.name # OK
d.eat() # Error !
參見:http://stackoverflow.com/questions/14299013/namespaces-within-a-python-class –