定义一个汽车类,属性可以有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输入属性值。
时间: 2024-06-13 18:08:31 浏览: 19
以下是定义一个汽车类并实例化两个对象的代码示例:
```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
```
1.定义一个汽车类,属性可以有颜色,品牌,车牌号,价格,并实例化 两个对象,给属性赋值,并输入属性值。
以下是定义一个汽车类,属性有颜色,品牌,车牌号,价格,并实例化两个对象给属性赋值,并输入属性值的示例代码:
```python
class Car():
def __init__(self, color, brand, id, price):
self.color = color
self.brand = brand
self.id = id
self.price = price
def say(self):
print("颜色:", self.color)
print("品牌:", self.brand)
print("车牌号:", self.id)
print("价格:", self.price)
a = Car("白色", "宝马", 123456, "56W")
b = Car("黑色", "梅德萨斯", 987654, "50W")
a.say()
b.say()
```
输出结果为:
```
颜色: 白色
品牌: 宝马
车牌号: 123456
价格: 56W
颜色: 黑色
品牌: 梅德萨斯
车牌号: 987654
价格: 50W
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)