3、创建一个名为"BankAccount"的类,该类包含私有属性"accountNumber"和"balance", 以及公有方法"getAccountNumber"、"getBalance"、"deposit"和"withdraw"来获取和操作私有属性的值。 在类的外部创建一个"BankAccount"对象并进行一些存款和取款操作,然后打印账户号码和余额。
时间: 2023-06-29 08:11:23 浏览: 225
下面是实现该题目的代码:
```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 amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient balance!")
# 创建一个BankAccount对象
account = BankAccount("123456789", 1000)
# 存款和取款操作
account.deposit(500)
account.withdraw(200)
# 打印账户号码和余额
print("Account Number:", account.getAccountNumber())
print("Balance:", account.getBalance())
```
输出结果为:
```
Account Number: 123456789
Balance: 1300
```
阅读全文