不确定我为什么会收到NameError:name' Automobile'没有定义。 也许在过去的20分钟内没有盯着这个的人可能会有所帮助。我开始认为它可能是缩进,但还不太确定。
vehicles.py:
class Automobile:
def __init__(self, make, model, mileage, price):
self.__make = make
self.__model = model
self.__mileage = mileage
self.__price = price
def set_make(self, make):
self.__make = make
def set_model(self, model):
self.__model = model
def set_mileage(self, mileage):
self.__mileage = mileage
def set_price(self, price):
self.__price = price
def get_make(self):
return self.__make
def get_model(self):
return self.__model
def get_mileage(self):
return self.__mileage
def get_price(self):
return self.__price
class Car(Automobile):
def __init__(self, make, model, mileage, price, doors):
Automobile.__init__(self, make, model, mileage, price)
self.__doors = doors
def set_doors(self, doors):
self.__doors = doors
def get_doors(self):
return self.__doors
car_demo.py:
import vehicles
def main():
used_car = vehicles.Car("Audi", 2007, 12500, 21500.00, 4)
print('Make:', used_car.get_make())
print('Model:', used_car.get_model())
print('Mileage:', used_car.get_mileage())
print('Price:', used_car.get_price())
print('# of doors:', used_car.get_doors())
main()
错误:
Traceback:
import vehicles, class Automobile:, and class Car(Automobile)
NameError: name 'Automobile' is not defined
我会认为将汽车定义为一个班级定义'汽车' 任何帮助将不胜感激。
我使用的是Python 3.5.2 IDLE
答案 0 :(得分:2)
我认为你并不意味着缩进Car
内的Automobile
类。您应该取消缩进该类一级,而不是:
Automobile.__init__(self, make, model, mileage, price)
你应该使用:
super(Car, self).__init__(make, model, mileage, price)
如果你正在使用Python 3,你可以这样做:
super().__init__(make, model, mileage, price)