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('%s is running'%self.name) class Subway(Vehicle): def __init__(self,name,speed,load,power,line): # self.name=name # self.speed=speed # Self.load = Load # self.power = Power Vehicle. The __init__ (self, name, Speed, Load, Power) # subclass call parent class method, the above is that code, self is needed here, because it is in the object class method calls self.line = Line DEF show_info (Self): Print (the self.name, self.speed, self.load, self.power, self.line) DEF RUN (Self): Vehicle.run ( Self) # Note transmitting Self Line13 = Subway ( ' bjdt ' , ' 100km / S ' , 5000, ' E ' , ' 13 is # ' ) line13.show_info() line13.run()
》》》
bjdt 100km/s 5000 E 13#
bjdt is running
super () method
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('%s is running'%self.name) class Subway(Vehicle): def __init__(self,name,speed,load,power,line): # self.name=name # self.speed=speed # Self.load = Load # self.power = Power # Vehicle .__ the init __ (self, name, Speed, Load, Power) # parent method call subclass is part of the code above, self is needed here, because the method call in the object class super (). the __init__ (name, Speed, Load, Power) # super transfer method without self, parent class methods can invoke the super method self.line = Line DEF show_info (Self): Print (self.name, self.speed, self.load, self.power, self.line) DEF RUN (Self): # Vehicle.run (Self) Note # transfer Self Super () RUN (). # Super methods without pass self, a method can be called directly by a super parent method Line13 = Subway ( ' bjdt ' , '100km/s',5000,'E','13#') line13.show_info() line13.run()
>>>
bjdt 100km/s 5000 E 13#
bjdt is running