python7day

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_size
85:
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’))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值