以下のコード サンプルでは、派生クラス b で self.number を使用しており、number は a (基本クラス) で定義されています。データ メンバーが基本クラスでこのように定義されている場合、派生クラスから常にアクセスできますか?
Python 2.7 を使用しています。これは基本オブジェクトのメンバー変数を参照する正しい方法ですか?
class a(object):
def __init__(self, number):
self.number = number
print "a __init__ called with number=", number
def printme(self):
print self.number
class b(a):
def __init__(self, fruit):
super(b, self).__init__(1)
self.fruit = fruit
print "b __init__ called with fruit=", fruit
def printme(self):
print self.number
cl1 = a(1)
cl1.printme()
cl2 = b("apple")
cl2.printme()