本文主要是介绍9-9电瓶升级,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://www.dedecms.com/knowledge/program/other/2012/0731/4046.html
9-9
# 9-9 电瓶升级
# 书中代码一
class Car():def __init__(self, make, model, year):self.make = makeself.model = modelself.year = yearself.odometer_reading = 0def get_descriptive_name(self):long_name = str(self.year) + ' ' + self.make + ' ' + self.modelreturn long_name.title()def read_odometer(self):print("This car has " + str(self.odometer_reading) + " miles on it.")def update_odometer(self, mileage):if mileage >= self.odometer_reading:self.odometer_reading = mileageelse:print("You can't roll back an odometer!")def increment_odometer(self, miles):self.odometer_reading += miles# 书中代码二class Battery():def __init__(self, battery_size=70):self.battery_size = battery_sizedef describe_battery(self):print("This ca has a " + str(self.battery_size) + "-kWh battery.")def upgrade_battery(self):if self.battery_size != 85:self.battery_size = 85def get_range(self):if self.battery_size == 70:range = 240elif self.battery_size == 85:range = 270message = "This car can go approximately " + str(range)message += " miles on a full charge."print(message)# 书中代码三class ElectricCar(Car):def __init__(self, make, model, year):super().__init__(make, model, year)self.battery = Battery()def describe_battery(self):print("This car has a " + str(self.battery_size) + "-kWh battery.")my_tesla=ElectricCar('tesla','model s',2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
这篇关于9-9电瓶升级的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!