python的父類和子類中關於繼承的不同版本的寫法


 1 Python 2.7中的繼承  2 在Python 2.7中,繼承語法稍有不同,ElectricCar 類的定義類似於下面這樣:  3 class Car(object):  4     def __init__(self, make, model, year):  5         --snip--
 6 
 7 class ElectricCar(Car):  8     def __init__(self, make, model, year):  9         super(ElectricCar, self).__init__(make, model, year) 10         --snip--
11 函數super() 需要兩個實參:子類名和對象self 。為幫助Python將父類和子類關聯起來,這些實參必不可少。另外,在Python 2.7中使用繼承時,務必在定義父類時在括號內指定object12 
13 Python 3中的繼承 14 class Car(): 15     def __init__(self, make, model, year): 16         --snip--
17 
18 class ElectricCar(Car): 19     def __init__(self, make, model, year): 20     '''初始化父類的屬性'''
21         super().__init__(make, model, year) 22         --snip--

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM