我正在学习"Python速成教程手册“这本书。我已经完全按照书中的方式编写了代码,但仍然出错。你能告诉我下面的代码有什么问题吗:
我得到了'AttributeError:'ElectricCar‘对象没有’battery_size‘属性。
班车:
def __init__(self, make, model, year):
self.make=make
self.model=model
self.year=year
self.odometer_reading = 0
def get_discriptive_name(self):
long_name= f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print a statement showing the car's mileage."""
print(f"This car has {self.odometer_reading} miles on it.")
def update_odometer(self, mileage):
"""
Set the Odometer reading to the given value.
Reject the change if it attempts to roll the odometer back.
"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self,miles):
"""Add the given amount to the odometer reading."""
self.odometer_reading += miles
"""Inheritance from parent/superclass to child/subclass."""class ElectricCar(汽车):“表示汽车的各个方面,特定于电动汽车。”
def __intit__(self,make,model,year):
super().__init__(make,model,year)
self.battery_size = 75
def describe_battery(self):
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery.")my_tesla =ElectricCar(‘特斯拉’,‘型号s',2009)
print(my_tesla.get_discriptive_name())
my_tesla.describe_battery()
发布于 2020-06-30 02:26:49
class ElectricCar(Car):
def __intit__(self,make,model,year):
super().__init__(make,model,year)
self.battery_size = 75
def describe_battery(self):
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery.")如果您查看一下,就会发现init函数拼写错误
https://stackoverflow.com/questions/62644393
复制相似问题