class Car( ):
“”“一次模拟汽车的尝试”""
def init(self,make,model,year):
#初始化描述汽车的属性
self.make =make
self.model = model
self.year = year
self.odometer_reading=0
def get_description_name(self):
#返回整洁的描述性信息
long_name=str(self.year)+’ ‘+self.make+’ '+self.model
return 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= mileage
else:
print(“you can`t roller back a odometer”)
def increment_odemeter(self,miles):
self.odometer_reading +=miles
class Battery( ):
def init(self,battery_size=70):
self.battery_size=battery_size
def describe_battery(self):
print("This car has "+str(self.battery_size))
def get_range(self):
if self.battery_size70:
range=240
elif self.battery_size85:
range=270
message=“This car go “+str(range)
message +=“miles on a full charge”
print(message)
class Electricar(Car):
def init(self,make,model,year):
“”“初始化父类的属性”””
super().init(make,model,year)#super超类,将父类和子类联系起来
self.battery= Battery()
my_tesla=Electricar(‘tesla’,‘model s’,2016)
print(my_tesla.get_description_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
print(hasattr(Car,‘read_odometer’))
print(hasattr(Battery,‘call’))
python7day
最新推荐文章于 2024-05-13 19:54:43 发布