我如何解决以下Python Oop测验 [英] How Do I Solve The Following Python Oop Quiz

查看:85
本文介绍了我如何解决以下Python Oop测验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#Create a class called BankAccount

#    Create a constructor that takes in an integer and assigns this to a `balance` #property.
#    Create a method called `deposit` that takes in cash deposit amount and #updates the balance accordingly.
#    Create a method called `withdraw` that takes in cash withdrawal amount and #updates the balance accordingly. if amount is greater than balance return #`"invalid transaction"`
#    Create a subclass MinimumBalanceAccount of the BankAccount class

#The Tests used to verify the procedure are as follows:***

import unittest
class AccountBalanceTestCases(unittest.TestCase):
  def setUp(self):
    self.my_account = BankAccount(90)
    
  def test_balance(self):
    self.assertEqual(self.my_account.balance, 90, msg='Account Balance Invalid')
    
  def test_deposit(self):
    self.my_account.deposit(90)
    self.assertEqual(self.my_account.balance, 180, msg='Deposit method inaccurate')
    
  def test_withdraw(self):
    self.my_account.withdraw(40)
    self.assertEqual(self.my_account.balance, 50, msg='Withdraw method inaccurate')
    
  def test_invalid_operation(self):
    self.assertEqual(self.my_account.withdraw(1000), "invalid transaction", msg='Invalid transaction')
  
  def test_sub_class(self):
    self.assertTrue(issubclass(MinimumBalanceAccount, BankAccount), msg='No true subclass of BankAccount')

推荐答案

请参阅 3。数据模型 - Python 3.3.6文档 [ ^ ]用于在Python中创建类。
See 3. Data model - Python 3.3.6 documentation[^] for creating classes in Python.


这篇关于我如何解决以下Python Oop测验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆