【问题标题】:Python - Multiple ClassesPython - 多个类
【发布时间】:2013-12-03 16:45:11
【问题描述】:

我收到了Vehicle.__init__(self, make, model, year, mileage, price, doors) TypeError: __init__() takes exactly 6 arguments (7 given)。我只输入6,所以我不确定发生了什么。我还需要一些关于我的库存功能的帮助。我正在尝试制作一个程序,允许用户将不同的车辆连同它们的某些属性一起输入到库存中。我非常接近完成这项工作,但可以使用一些额外的眼睛来希望看到我看不到的东西。任何建议将不胜感激。

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



class Car(Vehicle):

    #number of doors
    def __init__(self, make, model, year, mileage, price, doors):
        Vehicle.__init__(self, make, model, year, mileage, price, doors)
        self.__doors = doors
    def iDoors(self, doors):   
        self.__doors = doors
    def gDoors(self):
        return self.__doors


class Truck(Vehicle):

    #drive type (2 or 4 wheel drive)
    def __init__(self, make, model, year, mileage, price, drive):
        Vehicle.__init__(self, make, model, year, mileage, price, drive)
        self.__drive = drive
    def iDrive(self, drive):   
        self.__drive = drive
    def gDrive(self):
        return self.__drive


class SUV(Vehicle):

    #passanger capacity
    def __init__(self, make, model, year, mileage, price, passengers):
        Vehicle.__init__(self, make, model, year, mileage, price, passengers)
    def capacity(self, passengers):
        self.__passengers = passengers
    def gCapacity(self):
        return self.__passengers


class Inventory:

    def __init__(self, list1 = []):
        self.list1 = list1[:]
    def addVehicle(self, vehicle):
        self.list1.append(vehicle)
    def display(self):
        print("The inventory count is ", len(self.list1))
        for vehicle in self.list1:
            vehicle.display()


def main():

    inventory = Inventory()
    classType = input('Is the vehicle a car, truck, or suv?  ')
    if classType == 'car':
        make = input('Please enter the make of the car: ')
        model = input('Please enter the model of the car: ') 
        year = input('Please enter the year of the car: ')
        mileage = input('Please enter the mileage of the car: ')
        price = input('Please enter the price of the car: ')
        doors = input('Please enter the amount of doors on the car: ')
        car = Car(make, model, year, mileage, price, doors)
        print('Make: ', car.gMake())
        print('Model: ', car.gModel())
        print('Year: ', car.gYear())
        print('Mileage: ', car.gMileage())
        print('Price: ', car.gPrice())
        print('Number of doors: ', car.gDoors())
        print()
    elif classType == 'truck':
        make = input('Please enter the make of the truck: ')
        model = input('Please enter the model of the truck: ') 
        year = input('Please enter the year of the truck: ')
        mileage = input('Please enter the mileage of the truck: ')
        price = input('Please enter the price of the truck: ')
        drive = input('Please enter 2 wheel or 4 wheel drive for the truck: ')
        truck = Truck(make, model, year, mileage, price, drive)
        print('Make: ', truck.gMake())
        print('Model: ', truck.gModel())
        print('Year: ', truck.gYear())
        print('Mileage: ', truck.gMileage())
        print('Price: ', truck.gPrice())
        print('Type of drive: ', truck.gDrive())
        print()
    elif classType == 'suv':
        make = input('Please enter the make of the suv: ')
        model = input('Please enter the model of the suv: ') 
        year = input('Please enter the year of the suv: ')
        mileage = input('Please enter the mileage of the suv: ')
        price = input('Please enter the price of the suv: ')
        passengers = input('Please enter the capacity of the suv: ')
        suv = SUV(make, model, year, mileage, price, drive)
        print('Make: ', suv.gMake())
        print('Model: ', suv.gModel())
        print('Year: ', suv.gYear())
        print('Mileage: ', suv.gMileage())
        print('Price: ', suv.gPrice())
        print('Number of passengers: ', suv.gCapacity())
        print()
    cont = input('Would you like to add another vehicle?  y/n  ')
    if cont == 'y':
        main()
    elif cont == 'n':
        inventory.display()

【问题讨论】:

    标签: python class if-statement constructor


    【解决方案1】:

    您将所有参数从Car.__init__ 传递到Vehicle.__init__。问题是这两个函数没有相同的签名。

    class Car(Vehicle):
        #number of doors
        def __init__(self, make, model, year, mileage, price, doors):
            Vehicle.__init__(self, make, model, year, mileage, price)  # no doors
            self.__doors = doors
    

    【讨论】:

    • 为什么不使用super(Vehicle, self).__init__(make, model, year, mileage, price)
    • @qwertynl -- 至少出于学习目的,我认为这个版本更有指导意义。无需将super 的魔力拉入其中。一般来说,我不建议盲目使用super,因为讨论here。这并不意味着它没有位置——它确实有……但更多时候,我倾向于避免它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2017-11-29
    • 2011-02-07
    • 2022-10-17
    • 1970-01-01
    • 2018-04-18
    相关资源
    最近更新 更多