python super()方法

from django.test import TestCase

# Create your tests here.

#原生的解决办法   1
'''
class Father(object):
    def __init__(self):
        print('我是父类')


class Son(Father):
    def __init__(self):
        print('我是子类')
        Father.__init__(self)   #这个self必须手动传入


son_obj=Son()  
# 我是子类
# 我是父类
'''
'''
# python  2版本  super方法—调用父类的方法没有额外的参数   2

class Father(object):
    def __init__(self):
        print('我是父类')


class Son(Father):
    def __init__(self):
        print('我是子类')
        super(Son,self).__init__()    #要在super()方法中传入参数
son_obj=Son()

'''

'''
# python  2版本  super方法-调用父类的方法有额外的参数   3

class Father(object):
    def __init__(self):
        pass
    def run(self,road):
        print('running on %s'%road)


class Son(Father):
    def __init__(self):
        print('我是子类')

        super(Son,self).run('阳光路')  #需要把参数穿进去,如果调用的是父类的   类方法,self要改为 cls
son_obj=Son()
#  我是子类
# running on 阳光路

'''



# python  3版本  super方法   4


class Father(object):
    def __init__(self):
        pass
    def run(self,road):
        print('running on %s'%road)


class Son(Father):
    def __init__(self):
        print('我是子类')

        super().run('阳光路')   #super()方法不用传参数
son_obj=Son()
# 我是子类
# running on 阳光路

 

posted @ 2018-12-06 16:47  团子emma  阅读(103)  评论(0)    收藏  举报