定义一个交易类,记录每次交易账户,交易时间等
时间: 2024-05-11 10:20:14 浏览: 12
以下是一个简单的 Python 代码示例,定义了一个名为 Transaction 的交易类,它包含了账户、交易时间等属性和方法:
```python
class Transaction:
def __init__(self, account, amount, time):
self.account = account
self.amount = amount
self.time = time
def __str__(self):
return f"Transaction of {self.amount} dollars on account {self.account} at {self.time}"
```
在这个交易类中,我们定义了三个属性:账户(account)、交易金额(amount)和交易时间(time)。我们在类的构造函数 `__init__()` 中初始化了这些属性,并且定义了一个方法 `__str__()` 用于返回这个交易的信息。
使用示例:
```python
transaction = Transaction("John", 100, "2021-05-01 10:00:00")
print(transaction)
```
输出结果:
```
Transaction of 100 dollars on account John at 2021-05-01 10:00:00
```
这样,我们就可以创建一个 Transaction 类的实例来记录每次交易的信息了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)