面向对象的核心为对象,对象是由类实例化而来的,那么类与类之间存在一个继承的关系,被继承的类叫做父类,继承了父类的类为子类。
子类继承了父类,那么子类实例化的对象就可以调用所有父类的方法,当然也可以调用子类自身所有的方法。因为这些方法都属于该对象的方法。
比如,子类child继承了父类father
child.py
from father import father
class child(father):
def childprint(self):
print "this is a child"
father.py
class father:
def common(self):
print "this is a father also common"
那么当子类的一个实例test = child()可以调用父类的common()方法,如下所示:
third.py
from child import child
test = child()
test.common()
则运行third.py的结果为:
但如果我们将father.py的文件做如下修改:
father.py
class father:
def common(s