我正在为新手使用Python,我遇到了一个问题,我必须创建一个类和子类,这很好(我想我做得对)
但我现在必须使用 python unittest
进行一些测试模块,我不知道如何实现它,任何帮助将不胜感激。
class BankAccount:
def __init__(self):
self.balance = 0
def withdraw(self,amount):
if self.balance - amount <= 0:
print "Overdrawn, please try another option."
else:
self.balance -= amount
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance
def interest(self, amount):
percent = self.balance / 100.0 * amount
self.balance = self.balance + percent
return self.balance
class CreditAccount(BankAccount):
def withdraw(self,amount):
if self.balance - amount <= 0:
self.balance = self.balance - amount - 5
print "Overdrawn, you have been charged £5 for this."
return self.balance
else:
self.balance -= amount
return self.balance
class StudentAccount(BankAccount):
def __init__(self):
self.balance = 500
def withdraw(self, amount):
if self.balance - amount >= -3000:
self.balance -= amount
return self.balance
else:
print "£3000 Overdraft limit reached"
return self.balance
account = BankAccount()
account1 = CreditAccount()
account2 = StudentAccount()
account2.deposit(500)
最佳答案
让我开始吧..
my_account = BankAccount()
balance = my_account.deposit(1000) # can also be accessed by my_account.balance
希望你能从这里拿走它
关于Python 类和单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9882783/