2013-12-11 58 views
1

我做了一个程序,让用户创建具有特定属性的新车。现在我需要创建一个清单功能,将每个创建的车辆添加到所有车辆列表中,然后显示它们。这是我的,但它不起作用。我应该更具体地查看库存功能吗?Python类使对象列表

class Inventory: 

    def __init__(self, list1 = []): 
     self.list1 = list1[:] 
    def addVehicle(self, vehicle): 
     self.list1.append(vehicle) 
    def Display(self): 
     print(self.list1) 
+3

在Python之前,您是否碰巧学习过另一种面向对象的语言,其中一个getters和setters被推荐的练习? – DSM

+2

它不起作用?回溯?错误的输出? –

+0

当我运行主程序时,它唯一会输出的是[]'而不是所有车辆的列表。 – user3014014

回答

1

我想你应该缩短你的代码做这样如下(但绝​​对没有必要,因为据我可以看到它,使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() 
+0

这是与OOP相反的一种反模式。当每个对象都有一个类型时,为什么给它一个叫做type的属性?恕我直言,真的没有好的做法。 – Hyperboreus

+0

@Hyperboreus接受的论据。我改变了我的代码。我的想法是缩短重复的代码,但我做得太过分了。 – eyquem

0

看起来你可以使用类变量。我建议你添加一些小码到您Vehicle -class:

类车辆:

all_vehicles = [] 

def __init__(self, whatever): 
    # Your code 
    # … 
    # and now you just add the vehicle to the list of all vehicles. 
    # stored in a class variable 
    Vehicle.all_vehicles.append(self) 

如果你想打印所有的车辆,你只需要

print Vehicle.all_vehicles 

但这只是如果你在每个子类中调用车辆的方法__init__(因为它应该在编码良好的范围内)。