在學習python,和其它語言類似,python也支持繼承和多態。在OPP程序編程的時候,我們在定義一個class時,可以從一個現有的class繼承,新定義的這個class稱爲子類,原來的class稱爲父類、基類、超類。
class Father(object):
def run(self):
print ''Father is running"
現在從Father繼承一個類
class children1(Father):
pass
這樣,children1這個類不用定義run()方法就可以直接使用,繼承的另外一個優點就是可以自己新的方法,使代碼改善
class children1(Father):
def run(self):
print "children1 is running"
這樣,在代碼執行的時候,子類的run()方法會覆蓋父類的run()方法,這樣就構成了多態。