super()类是一个特殊的函数,这个函数是把父类和子类关联起来,super(ElectricCar, self).__init__(make,model,year)是调用父类的__init__方法,让子类的实例包含父类的所有方法,因为父类也称为超类,这就是super()的由来,
#父类 class Car(): def __init__(self,make,model,year): self.make = make self.model = model self.year = year self.odomete_reading = 0 def get_descriptive_name(self): long_name = str(self.year) + " " + self.make + " " +self.model return long_name def read_odometer(self): print("the car has " + str(self.odomete_reading) + " mlies!") def update_odometer(self,mlies): #修改参数odomete_reading的值 self.odomete_reading = mlies def increment_odometer(self,mlies): #通过内部的方法来使的内部数据增加 self.odomete_reading += mlies #子类 class ElectricCar(Car): #子类继承父类的书写方法 def __init__(self,make,model,year): super(ElectricCar, self).__init__(make,model,year) my_electricCar = ElectricCar('tesla','Q3',920) long_name = my_electricCar.get_descriptive_name() print(long_name) #920 tesla Q3 my_electricCar.update_odometer(100101) my_electricCar.read_odometer() #the car has 100101 mlies! my_electricCar.increment_odometer(2020) my_electricCar.read_odometer() #the car has 102121 mlies!
子类内部也可以定义属于自己的特有的方法:
#父类 class Car(): def __init__(self,make,model,year): self.make = make self.model = model self.year = year self.odomete_reading = 0 def get_descriptive_name(self): long_name = str(self.year) + " " + self.make + " " +self.model return long_name def read_odometer(self): print("the car has " + str(self.odomete_reading) + " mlies!") def update_odometer(self,mlies): #修改参数odomete_reading的值 self.odomete_reading = mlies def increment_odometer(self,mlies): #通过内部的方法来使的内部数据增加 self.odomete_reading += mlies #子类 class ElectricCar(Car): #子类继承父类的书写方法 def __init__(self,make,model,year): super(ElectricCar, self).__init__(make,model,year) self.battery_size = 70 #子类中特有的方法 def read_battery_size(self): print("the electeric car's battery is " + str(self.battery_size) + " sizes !") def update_battery_size(self,new_size): self.battery_size = new_size my_electricCar = ElectricCar('tesla','Q3',920) my_electricCar.read_battery_size() #the electeric car's battery is 70 sizes ! my_electricCar.update_battery_size(90) my_electricCar.read_battery_size() #the electeric car's battery is 90 sizes !
子类中可以重定义父类中的方法:
#父类 class Car(): def __init__(self,make,model,year): self.make = make self.model = model self.year = year self.odomete_reading = 0 def get_descriptive_name(self): long_name = str(self.year) + " " + self.make + " " +self.model return long_name def read_odometer(self): print("the car has " + str(self.odomete_reading) + " mlies!") def update_odometer(self,mlies): #修改参数odomete_reading的值 self.odomete_reading = mlies def increment_odometer(self,mlies): #通过内部的方法来使的内部数据增加 self.odomete_reading += mlies #子类 class ElectricCar(Car): #子类继承父类的书写方法 def __init__(self,make,model,year): super(ElectricCar, self).__init__(make,model,year) self.battery_size = 70 #子类中特有的方法 def read_battery_size(self): print("the electeric car's battery is " + str(self.battery_size) + " sizes !") def update_battery_size(self,new_size): self.battery_size = new_size #重定义父类的方法 def get_descriptive_name(self): long_name = str(self.year) + " " + self.make + " " +self.model + " and battery's size : " + str(self.battery_size) return long_name my_electricCar = ElectricCar('tesla','Q3',920) my_electricCar.read_battery_size() #the electeric car's battery is 70 sizes ! my_electricCar.update_battery_size(90) my_electricCar.read_battery_size() #the electeric car's battery is 90 sizes ! long_name = my_electricCar.get_descriptive_name() print(long_name) #920 tesla Q3 and battery's size : 90
将一个类的实例作为另一个类的属性,例如我们电车是一个类,我们电池也可以是一个类,但是在电车这个类中可以把电池这个类的实例作为电车这个类的属性
方便我们以后对电车的电池进行浮渣的操作,避免了把电车这个类弄得代码乱乱的
#父类 class Car(): def __init__(self,make,model,year): self.make = make self.model = model self.year = year self.odomete_reading = 0 def get_descriptive_name(self): long_name = str(self.year) + " " + self.make + " " +self.model return long_name def read_odometer(self): print("the car has " + str(self.odomete_reading) + " mlies!") def update_odometer(self,mlies): #修改参数odomete_reading的值 self.odomete_reading = mlies def increment_odometer(self,mlies): #通过内部的方法来使的内部数据增加 self.odomete_reading += mlies #子类 class ElectricCar(Car): #子类继承父类的书写方法 def __init__(self,make,model,year): super(ElectricCar, self).__init__(make,model,year) self.battery_size = Battery() #把Battery类的一个实例作为ElectricCar类的一个属性,我们就可以在Battery中对电池进行操作 #重定义父类的方法 def get_descriptive_name(self): long_name = str(self.year) + " " + self.make + " " +self.model + " and battery's size : " + str(self.battery_size) return long_name class Battery(): def __init__(self): self.battery_size = 70 def updata_battery_size(self,new_size): self.battery_size = new_size def read_battery_size(self): print("the electeric car's battery is " + str(self.battery_size) + " sizes !") my_electricCar = ElectricCar('tesla','Q3',920) my_electricCar.battery_size.read_battery_size() #the electeric car's battery is 70 sizes !