[英]How to assign values to objects in Python
I'm practicing Object Oriented Programing and need an explanation.我正在练习 Object 面向编程,需要解释。 I'm still confused by OOP.
我仍然对 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?
我将如何定义“Chiron 模型”将花费 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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.