• 技术文章 >Python技术 >Python高级

    案例详解:Python类继承机制

    silencementsilencement2019-07-09 14:33:25原创1932

    下面实现一个类继承的小程序,下面一边结合代码一边介绍相关继承的知识。例子以车、汽车为例,车为父类、汽车为子类。

    一、定义父类Vehicle

    class Vehicle():
       def __init__(self,wheelcount, power):#构造方法,参数有轮子数和动力
           self.wheelcount,self.power,self.totaldistance = wheelcount,power,0
           #初始化行驶总里程为0
       def gettotaldistance(self):return self.totaldistance  #定义获取形式总里程的方法
       def drive(self,distance):#定义drive方法
               self.totaldistance += distance

    二、定义子类Car

    class Car(Vehicle):
       def __init__(self,wheelcount, power):
           super().__init__(wheelcount, power)
           Vehicle.__init__(self,wheelcount, power)
           super(Car,self).__init__(wheelcount, power)
           self.oil,self.oilcostperkm=0,0.1

    子类内首先重写了构造方法,注意:

    1.首先调用了超类的构造方法,为了说明超类方法的调用,代码将三种调用超类构造方法都实现了,实际上只要有一种就够了;

    2.超类构造方法中初始化了轮子数、动力以及总行驶里程。子类调用超类构造方法后,对于汽车又初始化了油量和每公里油耗。?

     def drive(self,distance):
           realdistance=min(distance,self.oil/self.oilcostperkm)
           super().drive(realdistance)
           self.oil -= realdistance*self.oilcostperkm
           print("车开了{}公里,目前邮箱存油{:.2f}升,目前车辆总里程:{}KM".format(realdistance,self.oil, 
           super().gettotaldistance())

    子类重写了父类的drive方法,本次只用了一种老猿推荐的方式调用父类的drive方法,重写的方法内根据油量确认了实际驾驶里程之后调用了父类的drive方法,同时对油量进行了调整,输出了一些车况信息,其中调用了父类的gettotaldistance()方法。

    def  oiling(self,oil):
           self.oil+=oil
           print("加油{}升,目前邮箱存油{:.2f}升".format(oil,self.oil))

    实现子类独有的加油方法,父类的车可以是畜力或人力等其他方式驱动就没有这个方法。

    def needoiling(self):
        if self.oil<5: return True
        else:return False

    实现子类独有的是否需要加油判断方法。

    def output(self):
        print("车子动力为{},100KM油耗{}升,车子累计行驶{}KM,油箱存油{:.2f}L".format(self.power,self.oilcostperkm*100, 
        super().gettotaldistance(),self.oil))

    实现子类独有的输出车况的方法,其中调用了父类的gettotaldistance()方法。

    到此为止整个子类的代码实现完成,它完全继承了父类方法gettotaldistance,采用重写+父类调用方式实现了drive和构造方法的继承,并实现了needoiling、oiling两个子类独有的方法,其实例变量self.wheelcount,self.power,self.totaldistance是从父类继承。

    三、调用的代码

    下面是使用该类定义的一个实例,

    car = Car(4,'汽油发动机')
    car.oiling(50)
    for i in range(1,100):
    print("***************第{}次循环************".format(i))
    car.oiling(random.randint(10,60)) ?#随机加油x升
    car.drive(random.randint(5,1000)) #随机驾驶x公里
    car.output() ?#输出车况信息
    if car.needoiling(): break ?#如果油不够了就结束循环

    四、 上述例子的完整源代码

    #coding:utf-8
    import random
    class Vehicle():
       def __init__(self,wheelcount, power):
           self.wheelcount,self.power,self.totaldistance = wheelcount,power,0
      
       def drive(self,distance):
           self.totaldistance += distance  
           
       def gettotaldistance(self):return self.totaldistance
    
    class Car(Vehicle):
       def __init__(self,wheelcount, power):
           super().__init__(wheelcount, power)
           Vehicle.__init__(self,wheelcount, power)
           super(Car,self).__init__(wheelcount, power)
           self.totaldistance,self.oil,self.oilcostperkm=0,0,0.1
     
       def drive(self,distance):
           realdistance=min(distance,self.oil/self.oilcostperkm)
           super().drive(realdistance)
           self.oil -= realdistance*self.oilcostperkm
           print("车开了{}公里,目前邮箱存油{:.2f}升,目前车辆总里程:{}KM".format(realdistance,
           self.oil,super().gettotaldistance()))
               
       def  oiling(self,oil):
           self.oil+=oil
           print("加油{}升,目前邮箱存油{:.2f}升".format(oil,self.oil))
      
       def needoiling(self):
           if self.oil<5: return True
           else:return False
           
       def output(self):
           print("车子动力为{},100KM油耗{:.2f}升,车子累计行驶{}KM,油箱存油{:.2f}L".format(self.power,self.oilcostperkm*100,super().gettotaldistance(),self.oil))
    
    
    car = Car(4,'汽油发动机')
    car.oiling(50)
    for i in range(1,100):
       print("***************第{}次循环************".format(i))
       car.oiling(random.randint(10,60)) 
       car.drive(random.randint(5,1000))
       car.output()
       if car.needoiling(): break
    专题推荐:继承
    品易云
    上一篇:神秘而强大的Python生成器精讲 下一篇:python中的装饰器的使用实战

    相关文章推荐

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网