python初始化问题

2015-08-12  本文已影响52人  lovePython

子类和父类都__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__方法

上一篇 下一篇

猜你喜欢

热点阅读