簡體   English   中英

Python,獲取AttributeError:“ ElectricCar”對象沒有屬性“ battery”

[英]Python, getting AttributeError: 'ElectricCar' object has no attribute 'battery'

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()

_init__inti_類ElectricCar

拼寫錯誤,所以不是__inti__ ,而是ElectricCar類中的__init__ ,所以

所以然后: ... class ElectricCar(Car): def init (self, make, model, year): super().__init__(make, model, year) self.battery = Battery() ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM