我想你應該縮短你的代碼做這樣如下(但絕對沒有必要,因爲據我可以看到它,使inventory
一個類的實例)
class Vehicle:
def __init__(self, make, model, year, mileage, price):
self.__make = make
self.__model = model
self.__year = year
self.__mileage = mileage
self.__price = price
def iMake(self, make):
self.__make = make
def iModel(self, model):
self.__model = model
def iYear(self, year):
self.__year = year
def iMileage(self, mileage):
self.__mileage = mileage
def iPrice(self, price):
self.__price = price
def getMake(self):
return self.__make
def getModel(self):
return self.__model
def getYear(self):
return self.__year
def getMileage(self):
return self.__mileage
def getPrice(self):
return self.__price
def Display_Vehicle(self):
print('Inventory unit: %s' % self.__class__.name)
print('Make: ' , self.__make)
print('Model: ', self.__model)
print('Year: ' , self.__year)
print('Miles: ', self.__mileage)
print('Price: ', self.__price)
class Car(Vehicle):
name = 'Car'
def __init__(self,make, model, year, mileage, price, x):
Vehicle.__init__(self,make, model, year, mileage, price)
self.__doors = x
def iDoors(self, doors):
self.__doors = doors
def getDoors(self):
return self.__doors
def Display(self):
self.Display_Vehicle()
print('Number of doors: ', self.__doors)
class Truck(Vehicle):
name = 'Truck'
def __init__(self,make, model, year, mileage, price, x):
Vehicle.__init__(self,make, model, year, mileage, price)
self.__drive = x
def iDrive(self, drive):
self.__drive = drive
def getDrive(self):
return self.__drive
def Display(self):
self.Display_Vehicle()
print('Drive type: ', self.__drive)
class SUV(Vehicle):
name = 'SUV'
def __init__(self,make, model, year, mileage, price, x):
Vehicle.__init__(self,make, model, year, mileage, price)
self.__passengers = x
def iCapacity(self, passengers):
self.__passengers = passengers
def getCapacity(self):
return self.__passengers
def Display(self):
self.Display_Vehicle()
print('Number of passengers: ', self.__passengers)
def main():
inventory = []
while True:
classType = input('Is the vehicle a car, truck, or suv? ')
print (classType)
make = input('Please enter the make of the %s: ' % classType)
model = input('Please enter the model of the %s: ' % classType)
year = input('Please enter the year of the %s: ' % classType)
mileage = input('Please enter the mileage of the %s: ' % classType)
price = input('Please enter the price of the %s: ' % classType)
if classType == 'car':
x = input('Please enter the amount of doors on the car: ')
inventory.append(Car(make, model, year, mileage, price, x))
elif classType == 'truck':
x = input('Please enter 2 wheel or 4 wheel drive for the truck: ')
inventory.append(Truck(make, model, year, mileage, price, x))
elif classType == 'suv':
x = input('Please enter the capacity of the suv: ')
inventory.append(SUV(make, model, year, mileage, price, x))
print('\n\n')
inventory[-1].Display()
cont = 'go'
while cont not in ('y','n'):
cont = input('Would you like to add another vehicle? y/n ')
if cont == 'n':
print(inventory)
break
if __name__ == '__main__':
main()
在Python之前,您是否碰巧學習過另一種面向對象的語言,其中一個getters和setters被推薦的練習? – DSM
它不起作用?回溯?錯誤的輸出? –
當我運行主程序時,它唯一會輸出的是[]'而不是所有車輛的列表。 – user3014014