2013-12-11 55 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__(因爲它應該在編碼良好的範圍內)。