各位大佬我想知道一下这串代码有什么问题,从书上抄的不知道为什么提示'Battery' object has no attribute 'describe_battery'
希望帮忙看一下非常感谢 源代码: class Car():
"""一次模拟汽车的简单测试""" - def __init__(self,make,model,year):
- """初始化描述汽车的属性"""
- self.make=make
- self.model=model
- self.year=year
- self.odometer_reading=0
- def get_descriptive_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 roll back an odometer!")
- def increment_odometer(self,miles):
- self.odometer_reading += miles
- class Battery():
- def __init__(self,battary_size=70):
- '''初始化电瓶属性'''
- self.battery=battary_size
- class ElectricCar(Car):
- """
- 电动汽车的独特之处
- 初始化父类的属性,再初始化电动汽车特有的属性
- """
- def __init__(self, make,model,year):
- super().__init__(make,model,year)
- self.battery=Battery()
- my_tesla = ElectricCar('tesla','model','2016')
- print(my_tesla.get_descriptive_name())
- my_tesla.battery.describe_battery()
复制代码
|