'child class' object 没有属性 'attribute_name'
'child class' object has no attribute 'attribute_name'
我无法检索仅由继承 child class 创建的变量的值。
此外,在 child class 的初始化中更改从 parent class 继承的变量的值不适用。
这是 parent class:
class Car():
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
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
这里是 child class:
class ElectricCar(Car):
def __init___(self, make, model, year):
super(ElectricCar, self).__init__(make, model, year)
self.odometer_reading = 100
self.battery_size = 70
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
def describe_battery(self):
print(self.battery_size)
现在,如果我尝试 运行 此代码:
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla)
print(my_tesla.describe_battery())
我得到以下异常:
<class '__main__.ElectricCar'>:
{'make': 'tesla', 'model': 'model s', 'year': 2016, 'odometer_reading': 0}
AttributeError: 'ElectricCar' object has no attribute 'battery_size'
我有一个 oddmeter_reading 变量,它在 parent class 中的值为 0。
我从 child class 改为 100,但它不适用。
另外,仅在 child class 中设置的变量 battery_size
不会在 init.
中创建
有什么问题吗?我错过了什么?
您的 ElectricCar
class 中有错字。您用 3 个下划线而不是两个下划线定义了方法 __init___
,因此在创建新实例时不会调用它。
如果您将 ElectricCar
class 更改为:
class ElectricCar(Car):
def __init__(self, make, model, year): #Note the two underscores
super(ElectricCar, self).__init__(make, model, year)
self.odometer_reading = 100
self.battery_size = 70
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
def describe_battery(self):
print(self.battery_size)
那么这将是输出:
<class '__main__.ElectricCar'>: {'make': 'tesla', 'model': 'model s', 'year': 2016,
'odometer_reading': 100, 'battery_size': 70}
70
None
我无法检索仅由继承 child class 创建的变量的值。 此外,在 child class 的初始化中更改从 parent class 继承的变量的值不适用。
这是 parent class:
class Car():
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
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
这里是 child class:
class ElectricCar(Car):
def __init___(self, make, model, year):
super(ElectricCar, self).__init__(make, model, year)
self.odometer_reading = 100
self.battery_size = 70
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
def describe_battery(self):
print(self.battery_size)
现在,如果我尝试 运行 此代码:
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla)
print(my_tesla.describe_battery())
我得到以下异常:
<class '__main__.ElectricCar'>:
{'make': 'tesla', 'model': 'model s', 'year': 2016, 'odometer_reading': 0}
AttributeError: 'ElectricCar' object has no attribute 'battery_size'
我有一个 oddmeter_reading 变量,它在 parent class 中的值为 0。
我从 child class 改为 100,但它不适用。
另外,仅在 child class 中设置的变量 battery_size
不会在 init.
有什么问题吗?我错过了什么?
您的 ElectricCar
class 中有错字。您用 3 个下划线而不是两个下划线定义了方法 __init___
,因此在创建新实例时不会调用它。
如果您将 ElectricCar
class 更改为:
class ElectricCar(Car):
def __init__(self, make, model, year): #Note the two underscores
super(ElectricCar, self).__init__(make, model, year)
self.odometer_reading = 100
self.battery_size = 70
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
def describe_battery(self):
print(self.battery_size)
那么这将是输出:
<class '__main__.ElectricCar'>: {'make': 'tesla', 'model': 'model s', 'year': 2016,
'odometer_reading': 100, 'battery_size': 70}
70
None