python 定义一个汽车类(Car),属性有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输出属性值。 比如:属性:“红色”,“奔驰”,“黑A00000”,15000000
时间: 2023-06-11 17:06:09 浏览: 509
以下是实现代码:
```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("红色", "奔驰", "黑A00000", 15000000)
car2 = Car("白色", "宝马", "粤B12345", 12000000)
# 输出属性值
print("car1的属性值:")
print("颜色:", car1.color)
print("品牌:", car1.brand)
print("车牌号:", car1.license_plate)
print("价格:", car1.price)
print("car2的属性值:")
print("颜色:", car2.color)
print("品牌:", car2.brand)
print("车牌号:", car2.license_plate)
print("价格:", car2.price)
```
输出结果为:
```
car1的属性值:
颜色: 红色
品牌: 奔驰
车牌号: 黑A00000
价格: 15000000
car2的属性值:
颜色: 白色
品牌: 宝马
车牌号: 粤B12345
价格: 12000000
```
阅读全文