类(五)--子类中调用父类的属性--[点调用--扩展性差;super()调用--扩展性好]

class Vehicle:
    Country='China'
    def __init__(self,name,speed,load,power):  ##父类的数据属性
        self.name=name
        self.speed=speed
        self.load=load
        self.power=power
    def run(self):
        print('开动啦')
        print('开动啦')
class Subway(Vehicle):
        def __init__(self,name,speed,load,power,line):
           Vehicle.__init__(self,name,speed,load,power)   ##在子类的数据属性初始化时,加入父类的初始化。
           self.line=line                                 ##子类独有的数据属性

        def show_info(self):
            print(self.name,self.speed,self.load,self.power,self.line)

        def run(self):
            Vehicle.run(self)                             ##在调用父类属性时,如果父类名称改变,子类中的所有涉及调用父类的地方都需要改动,扩展性差
            print('%s %s 线,开动啦' %(self.name,self.line))
line13=Subway('北京地铁','10km/s',1000000000,'',13)

line13.show_info()

line13.run()

 

class Vehicle1:
    Country='China'
    def __init__(self,name,speed,load,power):
        self.name=name
        self.speed=speed
        self.load=load
        self.power=power
    def run(self):
        print('开动啦')
        print('开动啦')
class Subway(Vehicle1):
        def __init__(self,name,speed,load,power,line):
           # Vehicle.__init__(self,name,speed,load,power)
           # super().__init__(name,speed,load,power)  #super(__class__,self).__init__(name,speed,load,power)
           super(Subway,self).__init__(name,speed,load,power)##这三种写法都一样,就是把父类名称换成super()即可。
           self.line=line
        def show_info(self):
            print(self.name,self.speed,self.load,self.power,self.line)
        def run(self):
            # Vehicle.run(self)
            super().run()
            print('%s %s 线,开动啦' %(self.name,self.line))
line13=Subway('北京地铁','10km/s',1000000000,'',13)
line13.show_info()
line13.run()

print(line13.__class__)

 

posted @ 2020-04-19 21:25  无聊就看书  阅读(443)  评论(0编辑  收藏  举报