码迷,mamicode.com
首页 > 编程语言 > 详细

python的父类和子类中关于继承的不同版本的写法

时间:2018-08-10 17:58:38      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:back   定义类   col   class   size   需要   obj   括号   pytho   

 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--

 

python的父类和子类中关于继承的不同版本的写法

标签:back   定义类   col   class   size   需要   obj   括号   pytho   

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/9456421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!