Complete the Mint and Coin classes so that the coins created by a mint have the correct year and worth. Each Mint instance has a year stamp. The update method sets the year stamp to the current_year class attribute of the Mint class. The create method takes a subclass of Coin and returns an instance of that class stamped with the mint's year (which may be different from Mint.current_year if it has not been updated.) A Coin's worth method returns the cents value of the coin plus one extra cent for each year of age beyond 50. A coin's age can be determined by subtracting the coin's year from the current_year class attribute of the Mint class.
时间: 2024-02-29 07:53:22 浏览: 344
Mint UI 基于 Vue.js 移动端组件库
以下是Mint和Coin类的完整代码:
```python
class Mint:
current_year = 2021
def __init__(self, year):
self.year = year
def update(self):
self.year = self.current_year
def create(self, coin_class):
return coin_class(self.year)
class Coin:
def __init__(self, year):
self.year = year
def worth(self):
age = Mint.current_year - self.year
cents = self.cents
if age > 50:
cents += (age - 50)
return cents
class Quarter(Coin):
cents = 25
class Dime(Coin):
cents = 10
class Nickel(Coin):
cents = 5
class Penny(Coin):
cents = 1
```
在上述代码中,Mint类有一个current_year类属性和一个year实例属性。update方法用于将实例属性year更新为current_year类属性。create方法接受一个Coin子类,返回一个带有Mint年份的该子类的实例。
Coin类有一个year实例属性和一个worth方法。worth方法返回硬币的价值(以美分为单位),并根据其年龄增加额外的一分钱。Coin的子类Quarter、Dime、Nickel和Penny分别定义了硬币的价值(以美分为单位)。
注意,Mint.current_year是一个类属性,因此每个Mint实例都可以访问它。
阅读全文