代码里用到了
1.类的定义
2.子类继承父类
(1)如何重新定义子类的__init__()方法;
class BenzCar:
brand='奔驰'
@staticmethod #定义类的方法
def pressHorn()
print("嘟嘟~~")
def __init__(self,color,engineSN):
self.color=color
self.engineSN=engineSN
def change(self,newColor): #定义实例的方法
self.color=newColor
# 子类的继承
class Benz2016(BenzCar):
price = 580000
def __init__(self,color,engineSN,weight):#父类的参数也需要传
# 首先调用父类的初始化方法
BenzCar.__init__(self,color,engineSN)
self.weight = weight
self.oilweight = 0
def filloil(self,oilAdded):
self.oilweight +=oilAdded
self.weight += oilAdded
#子类的子类的继承
class Benz2016_01(Benz2016):
def __init__(self,color,engineSN,weight)
Benz2016.__init__(self,color,engineSN,weight)
car2 = Benz2016('red','111',1500)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)