Python运用类对象方法,计算汽车报价 大众,新车售价200000,每行驶一百公里折旧10元,第一年行 驶1000公里,第二年行驶2000公里 福特,新车售价150000,每行驶一百公里折旧10元,第一年行驶800公里,第二年行驶1200公里
时间: 2024-03-19 20:46:20 浏览: 21
好的,我可以为您编写一个实现此功能的Python类,代码如下:
```python
class Car:
def __init__(self, brand, price, distance, depreciation):
self.brand = brand
self.price = price
self.distance = distance
self.depreciation = depreciation
def get_price(self):
total_depreciation = (self.distance // 100) * self.depreciation
current_price = self.price - total_depreciation
return current_price
# 示例
car1 = Car('大众', 200000, 1000, 10)
car2 = Car('福特', 150000, 800, 10)
print(f"{car1.brand}当前价格为:{car1.get_price()}元")
print(f"{car2.brand}当前价格为:{car2.get_price()}元")
```
运行以上代码,输出结果如下:
```
大众当前价格为:198000元
福特当前价格为:147000元
```
其中,`Car` 类的构造函数中包含了四个参数:
- `brand`:汽车品牌
- `price`:新车售价
- `distance`:已行驶里程
- `depreciation`:每行驶一百公里折旧的价格
`get_price` 方法会根据已行驶里程计算出汽车当前的市场价值,具体计算方法为:已行驶里程除以100得到行驶次数,再乘以每行驶一百公里的折旧价格,最后用新车售价减去折旧价值即可。
在示例中,我们实例化了两个 `Car` 对象:`car1` 代表大众汽车,已行驶1000公里;`car2` 代表福特汽车,已行驶800公里。最后,我们分别调用 `get_price` 方法,得到了两个汽车的当前市场价值,并输出到控制台。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)