Python定义类的注意。

2018-12-24  本文已影响0人  电击小子_ea1b

class Car():

def __init__(self,make,model,year):

self.make = make

self.model = model

self.year = year

self.odometer = 0#设置汽车的里程数,若设置了初始值,就无需包含为它提供初始值的形参。

python创建子类时,父类必须包含在当前文件中,且位于子类前面.定义子类时,必须在括号中制定父类的名称.

class ElectricCar(Car):

def __init__(self,make,model,year):

super().__init__(make,model,year)#将父类和子类关联起来,使子类包含父类的所有属性

上一篇下一篇

猜你喜欢

热点阅读