''' 设计者:ISDF工软未来 版本:v4.0 日期:2024/3/7 ''' ''' 当子类细节过多时,可以再抽象一个类,并将类的实例作为子例的属性,这样子类可以通过.某类.方法去执行 ''' class Car:'''用户类'''def __init__(self,make,model,year):#类的属性self.make= makeself.model = modelself.year=yearself.odometer_reading=0def get_descriptive_name(self):print(f'{self.year} {self.make} {self.model}')def read_odometer(self):print(f'This car has{self.odometer_reading} miles on it')def update_odometer(self,mileage):if mileage>=self.odometer_reading:self.odometer_reading=mileageelse:print(f'你不能退后')def increment_odometer(self,miles):self.odometer_reading+=milesdef fillgasoil(self):print('{self.make}加20L油')class battery:def __init__(self,batterysize=100):self.battery_size=batterysizedef describe_battery(self):print(f'这车有1个{self.battery_size}-kwh 电池')def getjuli(self):if self.battery_size==100:range=200elif self.battery_size==300:range=500print(f'这个车可以跑{range}公里')class ElectricCar(Car):def __init__(self,make,model,year):'''初始哈父类属性'''super().__init__(make,model,year)self.battery=battery() def fillgasoil(self):'''电车需要充电,不加油,重写方法'''print('电车需要充电,不加油') def main():my_tesla=ElectricCar('changan','cs55',2019)my_tesla.battery.describe_battery()my_tesla.battery.battery_size=300my_tesla.battery.getjuli()if __name__ == '__main__':main()