简体   繁体   中英

Class variables in OOP

I am trying to develop a car-dealership-customer model to really understand OOP using Python.

The problem I am running into is that I'd like to define Car as a separate class object from the Dealer class. This causes confusion; how can I use Car attributes and upload them into the dealership inventory, for example, and then make transaction updates depending on customer acquisition?

I broke it down to just these two classes for now ( Car & Dealer ). I'd like to create an inventory dictionary with model being the key and the output from my retail_cost function being the cost of vehicle. How do i insert instances of car into the dealership inventory in the class dealer?

For example:

class Car(object):
    """This class basically defines a car in its simplest components
    and then computes a retail price in the method after initializing"""

    def __init__(self,model,engine,prod_cost):
        self.model = model
        self.engine = engine
        self.prod_cost = prod_cost

    def retail_cost(self):
        return self.prod_cost *1.20


class Dealer(object):
    """Defines the dealership itself with its inventory and sales components"""

    car_inventory = {}

    def __init__(self, dealer_name):
        self.dealer_name = dealer_name

The basic idea is that you add each instance of a car to the dealership, but note that I changed car_inventory to be an instance member of Dealer. This is because you want each new dealership you make to have its own car_inventory. If you left it as a class member of Dealer (the way you have it) then every dealership you make will have the same inventory.

class Car(object):
"""This class basically defines a car in its simplest components
and then computes a retail price in the method after initializing"""

def __init__(self,model,engine,prod_cost):
    self.model = model
    self.engine = engine
    self.prod_cost = prod_cost

def retail_cost(self):
    return self.prod_cost *1.20


class Dealer(object):
"""Defines the dealership itself with its inventory sales components"""

def __init__(self, dealer_name):
    self.dealer_name = dealer_name
    self.car_inventory = []



car1 = Car("ford", "fiesta", '18,000')
car2 = Car("ford", "fiesta", '12,000')

dealer = Dealer("tom's dealership")
dealer.car_inventory.append(car1)
dealer.car_inventory.append(car2)

print(dealer.car_inventory)

First, car_inventory should probably be an instance variable, not a class variable. This allows each dealer to have their own inventory.

def __init__(self, dealer_name):
    self.dealer_name = dealer_name
    self.car_inventory = {}

Now you can create Dealer objects:

dealer_alice = Dealer('Alice')
dealer_bob = Dealer('Bob')

Create Car objects:

car_1 = Car('Corolla', 'V4', 12000)
car_2 = Car('Focus', 'V4', 13000)

And add them to the dealers' inventories:

dealer_alice.car_inventory['Corolla'] = car_1
dealer_bob.car_inventory['Focus'] = car_2

Or instantiate new cars and add them directly to the inventories:

dealer_alice.car_inventory['Jetta'] = Car('Jetta', 'V6', 18000)
dealer_bob.car_inventory['Mustang'] = Car('Mustang', 'V8', 17000)

Each dealer now has an inventory dictionary with keys that are strings representing the model and values that are Car objects. You can print them like this:

for model,car in dealer_alice.car_inventory.items():
    print(model, car)

Note that this will print a nice model name like 'Jetta' , but the Car objects will only print basic information - namely, the name of the class and the object's location in memory. If you want nicer output, you'll have to define a __str__() method for the Car class. It would also be good to do that for the Dealer class so that you can just print(dealer_bob) and get tidy output.

I'd also recommend picking some other kind of key (like the VIN) - as it is, if the dealer gets another car of the same model it'll just overwrite the existing one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM