多重継承の場合《第2版》
統合された概念モデル(Python 2.2 以降)に沿って、object の傘下に新たなクラスを定義すると、組み込み型/利用者定義クラスの違いを区別せずに扱えます。
##---------------------------------------- multiple inheritance class Father(object): def __init__(self): self.name = "Adam" def show(self): return "Madam, I'm %s"%self.name class Mother(object): def __init__(self): self.age = 23 def show(self): return "I'm %d years old"%self.age class Son(Father, Mother): def __init__(self, name, age): self.name = name self.age = age def show(self): return super(self.__class__, self).show() class Daughter(Mother, Father): def __init__(self, name, age): self.name = name self.age = age def show(self): return super(self.__class__, self).show()
以下のコードを実行すると、
print "-"*40 for e in [ Father(), Mother(), Son("John Doe", 0), Daughter("Jane Doe", 0), ]: print "%s: %s"%(e.__class__.__name__, e.show())
次のような出力が得られます。
---------------------------------------- multiple inheritance Father: Madam, I'm Adam Mother: I'm 23 years old Son: Madam, I'm John Doe Daughter: I'm 0 years old
親クラスのメソッドを呼び出すときには、組み込み関数 Parent を利用できます。多重継承を認めているので、単に super などと記述できません。