2016-07-09 10 views
-3

私はElectricCarクラスにはバッテリー属性がないことを伝えるエラーが発生しているため、継承を学習して別のクラスのインスタンスを作成しています。誰かが私がここで行方不明を指摘してくれますか?私は数日の間この問題に取り組んできました。ここでPythonの継承:私のコードで何が問題になっていますか?

は誤りです:

Traceback (most recent call last): File "chapt 9 - Classes.py", line 367, in my_tesla.battery.describe_battery() AttributeError: 'ElectricCar' object has no attribute 'battery'

class Car(): 
    """A simple attempt to represent a car."""  
    def __init__(self, make, model, year): 
     """initialize attributes to describe a car.""" 
     self.make = make 
     self.model = model 
     self.year = year 
     self.odometer_reading = 0 

    def get_descriptive_name(self): 
     """Return a neatly formatted descriptive name.""" 
     long_name = str(self.year) + ' ' + self.make + ' ' + self.model 
     return long_name.title() 

    def read_odometer(self): 
     """Print a statement showing the car's mileage."""  
     print ("This car has " + str(self.odometer_reading) + " miles on it.") 

    def update_odometer(self, mileage): 
     """Set the odemeter 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 odemeter") 

    def increment_odometer(self, miles): 
     self.odometer_reading += miles 

class ElectricCar(Car): 
    def __init__(self, make, model, year): 
     super().__init__(make, model, year) 
     self.battery_size = Battery() 

    def describe_battery(self): 
     print ("This car has a " + str(self.battery_size) + "-kWh battery.") 

    def fill_gas_tank(): 
     print ("This car doesn't need a gas tank!") 


class Battery(): 
    def __init__(self, battery_size=70): 
     self.battery_size = battery_size 

    def describe_battery(self): 
     print ("This car has a " + str(self.battery_size) + "-kWh battery.") 

    def get_range(self): 
     """Print a statement about the range this battery provides.""" 
     if self.battery_size == 70: 
      range = 240 
     elif self.battery_size == 85: 
      range = 270 

    message = "This car can go approximately " + str(range) 
    message += " miles on a full charge." 
    print (message) 


def upgrade_battery(self): 
    if self.battery_size == 85: 
     self.battery_size = 85 
     print ("\nYour battery size is: " + self.battery_size + " You don't need an upgrade.") 
    else: 
     self.battery_size = 85 
     print ("\nYour battery size has been upgraded to: " + str(self.battery_size)) 

my_tesla = ElectricCar('tesla', 'model s', 2016) 
print (my_tesla.get_descriptive_name()) 
my_tesla.battery.describe_battery()  
my_tesla.battery.get_range()  

答えて

1

問題は、あなたのElectricCarクラスでは、あなたがBatteryクラスを初期化しているし、代わりにself.batteryの変数self.battery_sizeに設定することです。あなたのコードを変更する

class ElectricCar(Car): 
    def __init__(self, make, model, year): 
     super().__init__(make, model, year) 
     self.battery = Battery() # NOT self.battery_size 

はそれを動作させる必要があります。

関連する問題