父类方法太烂,不要,直接重写
这个是最简单的,直接写一个,跟父类函数名一样的方法就好了,举个栗子
Father.py
class Father(object):
def __init__(self):
pass
def getName(self):
print "father's name"
pass
Child.py
from Father import Father
class Child(Father):
def __init__(self):
super(Child, self).__init__()
def getName(self):
print "child's name"
if __name__ == '__main__':
c = Child()
c.getName()
执行结果如下
image.png父类方法已经很完美,直接用,自己懒得改
Father.py
class Father(object):
def __init__(self):
pass
def getName(self):
print "father's name"
pass
Child.py
from Father import Father
class Child(Father):
def __init__(self):
super(Child, self).__init__()
if __name__ == '__main__':
c = Child()
c.getName()
执行结果如下
image.png父类方法已经不错了,但是还少点东西,想自己加点
知识点来了,如何子类方法里,继续父类的故事,用super
Father.py
class Father(object):
def __init__(self):
pass
def getName(self):
print "father's name"
pass
Child.py
from Father import Father
class Child(Father):
def __init__(self):
super(Child, self).__init__()
def getName(self):
print "child's name"
if __name__ == '__main__':
c = Child()
c.getName()
执行结果如下
image.png父类方法已经很完美,直接用,自己懒得改
Father.py
class Father(object):
def __init__(self):
pass
def getName(self):
print "father's name"
pass
Child.py
from Father import Father
class Child(Father):
def __init__(self):
super(Child, self).__init__()
def getName(self):
super(Child, self).getName()
print "child's name"
if __name__ == '__main__':
c = Child()
c.getName()
执行结果如下
image.png干,就这个昨晚弄到一点,老是绕不过弯,早上突然顿悟,以前老是写super(Child, self).__init__()
,完全是抄过来,没有真正理会其中的意思,基础还是很差⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄
网友评论