class ElectricCar(Car): '''doc, blabla...''' def __init__(self, make, model, year): super().__init__(make, model, year); self.battery = Battery() def describe_battery(self): self.battery.describe_battery() # 重载函数 def fill_gas_tank(self): print('No gas tank for elactric car!')
def __init__(self,make,model,year): """初始化父类的属性,再初始化电动汽车特有的属性""" # super(ElectricCar, self).__init__(make,model,year) super().__init__(make,model,year) #super() 是一个特殊函数,帮助Python将父类和子类关联起来 self.battery = Battery(85)
def __init__(self, make, model, year): super().__init__(make, model, year) self.battery = Battery(70) self.__some_variable = 10
def __init__(self, make, model, year): super().__init__(make, model, year); self.battery = Battery()
"""import classes to the main program""" from car import Car from car import ElectricCar from car import Battery """Uses instances and objects in Car""" my_car=Car('b m w','320i',2008) print(my_car.description()) print(my_car.make.title()) """Uses instances and objects in ElectricCar""" my_volks=ElectricCar('audi','r8',2018) my_volks.des_batt() print(my_volks.model.title()) """Uses instances and objects in Battery""" my_tesla=Battery('tesla','model s',2018,'Lithian') my_tesla.des_battery() print(my_tesla.model.title())
from car import Car, ElectricCar my_beetle = Car('volkswagen', 'beetle', 2019) print(my_beetle.get_descriptive_name()) my_tesla = ElectricCar('tesla', 'roadster', 2019) print(my_tesla.get_descriptive_name()) import car my_beetle = car.Car('volkswagen', 'beetle', 2019) print(my_beetle.get_descriptive_name()) my_tesla = car.ElectricCar('tesla', 'roadster', 2019) print(my_tesla.get_descriptive_name()) from car import Battery battery1 = Battery(17) battery1.describe_battery() battery1.upgrade_battery() battery1.get_range() battery1.describe_battery() battery1 = Battery(18) battery1.describe_battery()
def __init__(self, make, model, year): # Initialize attributes of the parent class super().__init__(make, model, year) self.battery = Battery()