美文网首页
Python定义类的注意。

Python定义类的注意。

作者: 电击小子_ea1b | 来源:发表于2018-12-24 16:30 被阅读0次

    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)#将父类和子类关联起来,使子类包含父类的所有属性

    相关文章

      网友评论

          本文标题:Python定义类的注意。

          本文链接:https://www.haomeiwen.com/subject/krmzkqtx.html