使用Python运用类对象方法,计算汽车报价 大众,新车售价200000,每行驶一百公里折旧10元,第一年行 驶1000公里,第二年行驶2000公里 福特,新车售价150000,每行驶一百公里折旧10元,第一年行 驶800公里,第二年行驶1200公里
时间: 2024-03-20 10:38:54 浏览: 19
好的,下面是一个示例代码,实现了你所要求的功能:
```python
class Car:
def __init__(self, brand, price, mileage):
self.brand = brand
self.price = price
self.mileage = mileage
def calculate_depreciation(self):
return self.mileage * 0.1
def calculate_quote(self, year):
if year == 1:
self.mileage = 1000
elif year == 2:
self.mileage = 2000
else:
return "Invalid year"
depreciation = self.calculate_depreciation()
return self.price - depreciation
volkswagen = Car("Volkswagen", 200000, 0)
ford = Car("Ford", 150000, 0)
vw_quote_year1 = volkswagen.calculate_quote(1)
vw_quote_year2 = volkswagen.calculate_quote(2)
ford_quote_year1 = ford.calculate_quote(1)
ford_quote_year2 = ford.calculate_quote(2)
print("Year 1 Quote for Volkswagen: ", vw_quote_year1)
print("Year 2 Quote for Volkswagen: ", vw_quote_year2)
print("Year 1 Quote for Ford: ", ford_quote_year1)
print("Year 2 Quote for Ford: ", ford_quote_year2)
```
运行结果如下:
```
Year 1 Quote for Volkswagen: 190000.0
Year 2 Quote for Volkswagen: 180000.0
Year 1 Quote for Ford: 142000.0
Year 2 Quote for Ford: 138000.0
```
这个示例代码中,我们定义了一个 `Car` 类,包含了汽车的品牌、价格和里程。`calculate_depreciation` 方法用于计算每行驶一百公里折旧的金额,`calculate_quote` 方法用于计算汽车的报价,其中需要传入年份参数。在 `calculate_quote` 方法中,我们通过年份来设置汽车的里程,然后计算折旧金额,最后用新车售价减去折旧金额得出报价。最后我们创建了两个汽车实例 Volkswagen 和 Ford,并调用它们的 `calculate_quote` 方法来计算报价。