定义一个汽车类(Car),属性有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输入属性值。
时间: 2024-05-07 10:21:47 浏览: 53
以下是汽车类(Car)的定义以及实例化两个对象并赋值的代码:
```python
class Car:
def __init__(self, color, brand, license_plate, price):
self.color = color
self.brand = brand
self.license_plate = license_plate
self.price = price
# 实例化两个对象并赋值
car1 = Car("red", "BMW", "1234", 500000)
car2 = Car("blue", "Audi", "5678", 400000)
# 输出属性值
print("car1: color -", car1.color, ", brand -", car1.brand, ", license plate -", car1.license_plate, ", price -", car1.price)
print("car2: color -", car2.color, ", brand -", car2.brand, ", license plate -", car2.license_plate, ", price -", car2.price)
```
输出结果为:
```
car1: color - red , brand - BMW , license plate - 1234 , price - 500000
car2: color - blue , brand - Audi , license plate - 5678 , price - 400000
```
阅读全文