定义一个汽车类,属性可以有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输入属性值。
时间: 2024-06-13 08:08:31 浏览: 102
以下是定义一个汽车类并实例化两个对象的代码示例:
```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的属性值为:颜色:{},品牌:{},车牌号:{},价格:{}".format(car1.color, car1.brand, car1.license_plate, car1.price))
print("car2的属性值为:颜色:{},品牌:{},车牌号:{},价格:{}".format(car2.color, car2.brand, car2.license_plate, car2.price))
```
输出结果为:
```
car1的属性值为:颜色:red,品牌:BMW,车牌号:京A12345,价格:500000
car2的属性值为:颜色:black,品牌:Audi,车牌号:京B67890,价格:400000
```
相关问题
定义一个汽车类,属性可以有颜色,品牌,车牌号,价格,并实例化 两个对象,给属性赋值,并输入属性值。
以下是定义一个汽车类并实例化两个对象的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
```
定义一个汽车类,属性可以有颜色,品牌,车牌号,价格,并实例化 两个对象,给属性赋值,并输入属性值。
以下是定义一个汽车类并实例化两个对象的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
```
阅读全文