1.定义一个汽车类,属性可以有颜色,品牌,车牌号,价格,并实例化 两个对象,给属性赋值,并输入属性值。
时间: 2024-06-13 07:08:46 浏览: 14
以下是定义一个汽车类,属性有颜色,品牌,车牌号,价格,并实例化两个对象给属性赋值,并输入属性值的示例代码:
```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
```
相关问题
1.定义一个汽车类,属性可以有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输入属性值。
```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', 'Benz', '京B67890', 600000)
# 输出属性值
print('car1的颜色为:', car1.color)
print('car1的品牌为:', car1.brand)
print('car1的车牌号为:', car1.license_plate)
print('car1的价格为:', car1.price)
print('car2的颜色为:', car2.color)
print('car2的品牌为:', car2.brand)
print('car2的车牌号为:', car2.license_plate)
print('car2的价格为:', car2.price)
```
输出结果:
```
car1的颜色为: red
car1的品牌为: BMW
car1的车牌号为: 京A12345
car1的价格为: 500000
car2的颜色为: black
car2的品牌为: Benz
car2的车牌号为: 京B67890
car2的价格为: 600000
```
定义一个汽车类,属性可以有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输入属性值。
以下是定义一个汽车类并实例化两个对象的代码示例:
```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
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)