代码拉取完成,页面将自动刷新
class Car:
'''A simple attempt to simulate a car'''
def __init__(self,make,model,year):
'''Initialize the attributes that describe the car'''
self.make=make
self.model=model
self.year=year
self.odometer_reading=0
def get_descriptive_name(self):
'''Return neat descriptive informaiton'''
long_name=f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
'''Print a message indicating car mileage'''
print(f"This car has {self.odometer_reading} miles on it .")
#使用方法修改属性
def update_odometer(self,mileage):
'''
Set the odometer reading to the specified value
Do not reverse the odometer reading
'''
if mileage>=self.odometer_reading:
self.odometer_reading=mileage
else:#此处是禁止将里程表读数往回调
print("You can't roll back an odometer !")
#通过方法对属性的值进行递增
def increment_odometer(self,miles):
'''Increase the odometer reading by a specified amount
Suppress negative increments
'''
if miles>=0:
self.odometer_reading+=miles
else:
print("Error increase !")
class Battery:
'''A simple attempt to simulate electric vehicle battery'''
def __init__(self,battery_size=75):
self.battery_size=battery_size
def describe_battery(self):
'''Print a message describing the battery capcity'''
print(f"This car has a {self.battery_size}-kwh battery")
def get_range(self):
'''Print a message indicating the battery range'''
if self.battery_size==75:
range=20
elif self.battery_size==100:
range=315
class ElectricCar(Car):
'''The uniqueness of the electric car'''
def __init__(self,make,model,year):
'''
Initialize the properties of the parent class
Reinitialize unique attributes of electric vehicles
'''
super().__init__(make,model,year)
self.battery=Battery()
print(f"This car can go about {range} miles on a full charge")
my_tesla=ElectricCar('tesla','model s',2019)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。