美文网首页
python初始化问题

python初始化问题

作者: lovePython | 来源:发表于2015-08-12 16:47 被阅读52次

子类和父类都__init__

class Father(object):
        def __init__(self):
            print 'Father'
class Son(Father):
        def __init__(self):
            print 'Son'


son = Son()

运行结果是:

Son

如果子类有__init__则不会调用父类的__init__方法;

假如子类没有__init__方法,而父类有init方法


class Father(object):
        def __init__(self):
            print 'Father'
class Son(Father):
    pass

son = Son()

运行结果:

Father

如果子类没有__init__方法则会调用父类的__init__方法

多继承,父类都有__init__方法

class Father(object):
        def __init__(self):
            print 'Father'
class Father2(object):
        def __init__(self):
            print 'Father2'
class Son(Father2,Father):
    pass

son = Son()

运行结果:

Father2

多继承的时候只会调用第一个第一个父类的__init__方法

相关文章

网友评论

      本文标题:python初始化问题

      本文链接:https://www.haomeiwen.com/subject/fbwiqttx.html