Python2覚書 クラスの継承

クラスの継承

継承は次のように記述します。

class subClass(superClass)

実際に例を見てみましょう。Personというsuperクラスを用意します。

class Person:
    def __init__(self, name):
        self.__name = name
    
    def introduce(self):
        print self.__name
        print '''....'''

    def sleep(self):
        print self.__name + ":zzz"

PythonUser, CppUserという、Personクラスを継承したsubクラスを作成します。

PythonUser
class PythonUser(Person):
    def __init__(self, name):
        self.__subName = name
    
    def introduce(self):
        print self.__subName
        print '''I enjoy writing Python scripts.'''      
          
    def callCpp(self):
            print "hello Mr.cpp"
CppUser
class CppUser(Person):
    def __init__(self, name):
        self.__subName = name
    
    def introduce(self):
        print self.__subName
        print '''I enjoy writing algorithms using C++.'''
    
    def callPython(self):
            print "hello Mr.python"

オーバーライド

同じ名前のメンバ関数をサブクラスで改めて定義することをオーバーライドと呼びます。上の例ではintroduceをオーバーライドしています。

PythonUser
    def introduce(self):
        print self.__subName
        print '''I enjoy writing Python scripts.'''    
CppUser
    def introduce(self):
        print self.__subName
        print '''I enjoy writing algorithms using C++.'''

2つのクラスのインスタンスを生成し、メンバ関数を順に呼び出しながらクラス継承の機能を確認してみます。

Alice = PythonUser("Alice")
Bob = CppUser("Bob")

継承(サブクラスのメソッド呼び出し)


Alice.callCpp()
Bob.callPython()


hello Mr.cpp
hello Mr.python

多態性(オーバーライドしたメソッドの呼び出し)


Alice.introduce()
Bob.introduce()


Alice
I enjoy writing Python scripts.
Bob
I enjoy writing algorithms using C++.

継承による機能拡張、多態性の性質はともに実現されていることが分かります。

継承(superクラスのメソッド呼び出し)

Alice.sleep()


:zzz

superクラスのメソッドも呼び出せています。

継承時のsuperクラスの初期化

superクラスのsleepはこのような処理を行いますが、superクラスのメンバ変数"__name"に値が入っていません。

    def sleep(self):
        print self.__name + ":zzz"

subクラスと同時にsuperクラスが初期化されていない訳です。
そこでsubクラスの初期化メソッド内でsuperクラスの初期化メソッドを呼び出しておきます。(C++の初期化子に対応する処理を実装しなければならない)

PythonUser
class PythonUser(Person):
    def __init__(self, name):
        Person.__init__(self,name)       
        self.__subName = name

CppUser
class CppUser(Person):
    def __init__(self, name):
        Person.__init__(self,name)
        self.__subName = name

これでsubクラス側からsuperクラスの初期化を行うことができました。

Alice.sleep()


Alice:zzz

superクラスのメソッド呼び出し

subクラス内でsuperクラスのメソッドを呼び出すには、superクラスの識別子をメソッドの前に付けます。

superClassName.method()

多重継承

pythonは多重継承もサポートしています。多重継承は次のように記述します。

class subClass(superClass1,superClass2,...)

こちらも実際に確認してみます。

class Robot:
    __name = ""
    tall=0
    def __init__(self, name):
        self.__name = name
    
    def introduce(self):
        print self.__name
        print '''01101101'''
        
    def calculate(self):
        print self.__name + ":1+1=10"

先ほどのPersonクラスとRobotクラスを多重継承させた、Androidクラスを作成します。superクラスが2つあるので両方とも初期化しなければなりません。

class Android(Person,Robot):
    def __init__(self, name):
        Person.__init__(self, name)
        Robot.__init__(self, name)
        self.__name = name

    def introduce(self):
        print self.__name + ":"
        print '''My 0100 birthday 010!!'''


これで両方のクラスの機能を組み合わせることができました。

John = Android("John")
John.sleep()
John.calculate()
John.introduce()


John:zzz
John:1+1=10
John:
My 0100 birthday 010!!