python 类继承方法(案例详解:Python类继承机制)
下面实现一个类继承的小程序 ,下面一边结合代码一边介绍相关继承的知识 。例子以车 、汽车为例 ,车为父类 、汽车为子类 。
一 、定义父类Vehicle
classVehicle(): def__init__(self,wheelcount,power):#构造方法 ,参数有轮子数和动力 self.wheelcount,self.power,self.totaldistance=wheelcount,power,0 #初始化行驶总里程为0 defgettotaldistance(self):returnself.totaldistance#定义获取形式总里程的方法 defdrive(self,distance):#定义drive方法 self.totaldistance+=distance二 、定义子类Car
classCar(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.超类构造方法中初始化了轮子数 、动力以及总行驶里程。子类调用超类构造方法后 ,对于汽车又初始化了油量和每公里油耗 。?
defdrive(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()方法 。
defoiling(self,oil): self.oil+=oil print("加油{}升,目前邮箱存油{:.2f}升".format(oil,self.oil))实现子类独有的加油方法 ,父类的车可以是畜力或人力等其他方式驱动就没有这个方法 。
defneedoiling(self): ifself.oil<5:returnTrue else:returnFalse实现子类独有的是否需要加油判断方法 。
defoutput(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) foriinrange(1,100): print("***************第{}次循环************".format(i)) car.oiling(random.randint(10,60))?#随机加油x升 car.drive(random.randint(5,1000))#随机驾驶x公里 car.output()?#输出车况信息 ifcar.needoiling():break?#如果油不够了就结束循环 #coding:utf-8 importrandom classVehicle(): def__init__(self,wheelcount,power): self.wheelcount,self.power,self.totaldistance=wheelcount,power,0 defdrive(self,distance): self.totaldistance+=distance defgettotaldistance(self):returnself.totaldistance classCar(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 defdrive(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())) defoiling(self,oil): self.oil+=oil print("加油{}升,目前邮箱存油{:.2f}升".format(oil,self.oil)) defneedoiling(self): ifself.oil<5:returnTrue else:returnFalse defoutput(self): print("车子动力为{},100KM油耗{:.2f}升,车子累计行驶{}KM ,油箱存油{:.2f}L".format(self.power,self.oilcostperkm*100,super().gettotaldistance(),self.oil)) car=Car(4,汽油发动机) car.oiling(50) foriinrange(1,100): print("***************第{}次循环************".format(i)) car.oiling(random.randint(10,60)) car.drive(random.randint(5,1000)) car.output() ifcar.needoiling():break创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!