创建一个名为"BankAccount"的类,该类包含私有属性"accountNumber"和"balance", 以及公有方法"getAccountNumber"、"getBalance"、"deposit"和"withdraw"来获取和操作私有属性的值。 在类的外部创建一个"BankAccount"对象并进行一些存款和取款操作,然后打印账户号码和余额。
时间: 2023-06-28 18:11:42 浏览: 113
下面是一个示例代码,实现了所述功能:
```python
class BankAccount:
def __init__(self, accountNumber, balance):
self.__accountNumber = accountNumber
self.__balance = balance
def getAccountNumber(self):
return self.__accountNumber
def getBalance(self):
return self.__balance
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if self.__balance >= amount:
self.__balance -= amount
else:
print("Insufficient funds")
# 创建一个 BankAccount 对象
myAccount = BankAccount("123456789", 1000)
# 存入 500 元
myAccount.deposit(500)
# 取出 200 元
myAccount.withdraw(200)
# 打印账户号码和余额
print("Account number:", myAccount.getAccountNumber())
print("Balance:", myAccount.getBalance())
```
输出结果应该为:
```
Account number: 123456789
Balance: 1300
```
阅读全文