#子类中调用父类
#初步程序
# class Vehicle:#定义交通工具
# Country="China"
# def __init__(self,name,speed,load):
# self.name=name
# self.spedd=speed
# self.load=load
#
# def run(self):
# print("开动了")
#
# class Subway(Vehicle):#定义个地铁
# def __init__(self,name,speed,load,line):
# self.name=name
# self.spedd=speed
# self.load=load
# self.line=line
#
# def show_info(self):
# print(self.name,self.line)
#
# line13=Subway("北京地铁","100m/s","1000","13")
#
# line13.show_info()
#
# #子类调用父类
# class Vehicle:#定义交通工具
# Country="China"
# def __init__(self,name,speed,load):
# self.name=name
# self.spedd=speed
# self.load=load
#
# def run(self):
# print("开动了")
#
# class Subway(Vehicle):#定义个地铁
# def __init__(self,name,speed,load,line):
# Vehicle.__init__(self,name,speed,load)#引用父类的属性
# self.line=line
# def show_info(self):
# print(self.name,self.line)
#
# def run(self):
# Vehicle.run(self)#引用父类的函数属性
# print("%s %s x线"%(self.name,self.line))
#
# line13=Subway("北京地铁","100m/s","1000","13")
#
# line13.show_info()
# line13.run()
# 以上问题出现修改父类名称后面的都需要修改比较麻烦
# 下面使用supper来解决上面问题:不用传self参数 和 修改父类名其他不用改
class Vehicle1:#定义交通工具
Country="China"
def __init__(self,name,speed,load):
self.name=name
self.spedd=speed
self.load=load
def run(self):
print("开动了")
class Subway(Vehicle1):#定义个地铁
def __init__(self,name,speed,load,line):
super().__init__(name,speed,load)#通过super的方法直接调用父类属性
self.line=line
def show_info(self):
print(self.name,self.line)
def run(self):
super().run()#不用传自己的self
print("%s %s x线"%(self.name,self.line))
line13=Subway("北京地铁","100m/s","1000","13")
line13.show_info()
line13.run()