pythonクラス4:クラスの継承と親メソッドの上書き


pythonは継承し、子クラスは親クラスの様々な方法を上書きすることができます.init__方法.
親の__を上書きする場合init__メソッド、上書きメソッドで親を参照したい_init__親クラスの__などのメソッドinit__メソッドに基づいて、プロパティの設定やその他を追加します.
親を参照するメソッドを表示する必要があります.そうしないと、親のメソッドは直接呼び出されません.
例:
#coding:utf-8

# c   p,       __init__  ,        __init__  ,         
# c    __init__       p __init__  ,           p,    p 
# __init__    self  

class p(object):
    def __init__(self,nm):
        self.name  = nm

class c(p):
    def __init__(self,nm,idnum):
        p.__init__(self,nm)
        self.id  = idnum

c1 = c('john',11)
print c1
print c1.name,c1.id

実行結果:
[root@puppet-master-231-test eg_4]# python2.7  class_inherit_demo_4.py
<__main__.c>
john 11