Python类来创建对象列表

时间:2013-12-11 19:00:10

标签: python class

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

class Inventory:

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

3 个答案:

答案 0 :(得分:2)

您需要拨打inventory.addVehicle的某个地方(除非我在您的代码中遗漏了它),以便将最近创建的对象实际添加到您的广告资源中。

另一个问题是,您递归拨打main并在main的顶部覆盖inventory

也许你可以像这样重构它:

def main():
    inventory = Inventory()

    while True:
        classType = input('Is the vehicle a car, truck, or suv?  ')
        vehicle = None
        if classType == 'car':
            #inputs
            vehicle = Car(make, model, year, mileage, price, doors)
        elif classType == 'truck':
            #inputs
            vehicle = Truck(make, model, year, mileage, price, drive)
        elif classType == 'suv':
            #inputs
            vehicle = SUV(make, model, year, mileage, price, passengers)
        else: print('Unkown vehicle type')
        if vehicle: inventory.addVehicle (vehicle)
        cont = input('Would you like to add another vehicle?  */n  ')
        if cont == 'n': break
    inventory.Display()

旁注:你不需要所有那些getter和setter:省略双重下划线并直接写入和读取它们(我们都是成年人)。如果封装真的是一个问题,那么你可以使用@property@setter装饰器。

答案 1 :(得分: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()

答案 2 :(得分:0)

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

类车辆:

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__ - Vehicle方法时才有效(因为它应该在编码良好的oop中)。