我是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 –