定义一个汽车类,属性可以有颜色,品牌,车牌号,价格,并实例化 两个对象,给属性赋值,并输入属性值。
时间: 2024-06-13 14:08:27 浏览: 120
汽车的类定义
以下是定义一个汽车类并实例化两个对象的Python代码:
```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', '京A12345', 500000)
car2 = Car('black', 'Audi', '京B67890', 400000)
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的属性值为:
颜色: red
品牌: BMW
车牌号: 京A12345
价格: 500000
car2的属性值为:
颜色: black
品牌: Audi
车牌号: 京B67890
价格: 400000
```
阅读全文