Python,获取AttributeError:“ ElectricCar”对象没有属性“ battery”

时间:2018-09-21 00:25:29

标签: python python-3.x

class Car:
    def __init__(self, year, make, model):
        self.year = year
        self.make = make
        self.model = model
        self.odometer_reading = 0
    def get_descriptive_name(self):
        long_name = str(self.year) + ' ' + str(self.make) + ' ' +str(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):
        self.odometer_reading = mileage
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print('you cant roll it back')


class Battery():
    """A simple attempt to model a battery for an electric car."""
    def __init__(self, battery_size=70):
        self.battery_size = battery_size
    def describe_battery(self):
        " ""Print a statement describing the battery size."""
        print("This car has a " + str(self.battery_size) + "-kWh battery.")

#The call to the battery attribute in the ElectricCar class is where the         
#error emanates
class ElectricCar(Car):
    def __inti__(self, make, model, year):
        super().__init__(make, model, year)
        self.battery = Battery()


my_telsa = ElectricCar('Volvo', 'models s', 2006)
print(my_telsa.get_descriptive_name())
my_telsa.battery.describe_battery()

2 个答案:

答案 0 :(得分:0)

在ElectricCar类中,它是_init_而不是_inti_

答案 1 :(得分:0)

拼写错误,所以不是__inti__而是__init__类中的ElectricCar,所以

然后: ... class ElectricCar(Car): definit(self, make, model, year): super().__init__(make, model, year) self.battery = Battery() ...