覆盖使用python中的继承方法访问的属性

时间:2016-09-17 14:12:40

标签: python inheritance

我尝试继承一个方法并使用它来访问我的子类中定义的变量。 这是一个非常简单的例子:

class A(object):
    def __init__(self):
        self.__type='parent'
    def type(self):
        return self.__type

class B(A):
    def __init__(self):
        super(B,self).__init__()
        self.__type='child'

x=B()
print x.type()

Python返回' parent'作为结果。 我假设这与在类A范围内定义的方法类型有关,并且只能访问在类A中定义的内容。

什么被认为是避免在我的子类中定义我的方法的最优雅的方法?

0 个答案:

没有答案