简体   繁体   中英

How to assign values to objects in Python

I'm practicing Object Oriented Programing and need an explanation. I'm still confused by OOP. I've decided to create a car configurator that will calculate the total cost of users choice but I have a problem defining the prices. How would I go defining that 'Chiron model' would cost $3,000,000? How would I limit the color choice to let's say "black", "white" and "red"? And assign a different price value to them?

class Vehicle:
    def __init__(self,name,model,year,color):
        self.name = name
        self.model = model
        self.year = year
        self.color = color

class Model:
    def __init__(self,chiron,veyron):
        self.chiron = chiron
        self.veyron = veyron

class Color:
    def __init__(self,color):
        self.color = color
class Vehicle:
    def __init__(self,name,model,year,color):
        self.name = name
        self.model = model
        self.year = year
        self.color = color

class Model:
    def __init__(self,chiron,veyron):
        self.chiron = chiron
        self.veyron = veyron

class Color:
    def __init__(self,color):
        self.color = color

if __name__ == '__main__':  # add this
    class_Vehicle = Vehicle(name='BMW', model='x5', year=2021, color='white')

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