定义一个汽车类,属性可以有颜色,品牌,车牌号,价格,并实例化 两个对象,给属性赋值,并输入属性值。
时间: 2024-06-13 09:08:46 浏览: 81
汽车的类定义
以下是定义一个汽车类并实例化两个对象的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", "Toyota", "A12345", 100000)
car2 = Car("blue", "Honda", "B67890", 80000)
print("Car 1:")
print("Color:", car1.color)
print("Brand:", car1.brand)
print("License Plate:", car1.license_plate)
print("Price:", car1.price)
print("Car 2:")
print("Color:", car2.color)
print("Brand:", car2.brand)
print("License Plate:", car2.license_plate)
print("Price:", car2.price)
```
输出结果为:
```
Car 1:
Color: red
Brand: Toyota
License Plate: A12345
Price: 100000
Car 2:
Color: blue
Brand: Honda
License Plate: B67890
Price: 80000
```
阅读全文