创建一个名为 `BankAccount` 的类,包含以下属性:`balance` 和 `account_number`。创建一个类方法 `create_account()`,用于创建一个新的银行账户。请给出使用示例。
时间: 2023-10-22 17:09:51 浏览: 326
1.编写一个类实现银行帐户的概念,包括的属性有“帐号
5星 · 资源好评率100%
```python
class BankAccount:
account_number = 0
def __init__(self, balance):
self.balance = balance
BankAccount.account_number += 1
self.account_number = BankAccount.account_number
@classmethod
def create_account(cls, initial_balance):
return cls(initial_balance)
account1 = BankAccount.create_account(1000)
print(f"Account number {account1.account_number} has a balance of {account1.balance}")
account2 = BankAccount.create_account(5000)
print(f"Account number {account2.account_number} has a balance of {account2.balance}")
```
输出:
```
Account number 1 has a balance of 1000
Account number 2 has a balance of 5000
```
阅读全文