PythonでBankAccount
オブジェクト指向向けの「踏み絵」的な課題らしいです。
#encoding:shift-jis from __future__ import with_statement, division, print_function class BankAccount(object): """ >>> import bank_account >>> a = bank_account.BankAccount(0) >>> a.dollars = 200 >>> a.deposit(50) >>> a.dollars 250 >>> a.withdraw(100) >>> a.dollars 150 >>> a.withdraw(200) >>> a.dollars 0 """ def __init__(self, dollars): self._dollars = dollars @property def dollars(self): return self._dollars @dollars.setter def dollars(self, v): self._dollars = v if v > 0 else 0 def deposit(self, x): self.dollars += x def withdraw(self, x): self.dollars -= x class StockAccount(BankAccount): """ >>> import bank_account >>> stock = bank_account.StockAccount(10, 30) >>> stock.numShares 10 >>> stock.pricePerShare 30 >>> stock.dollars 300 >>> stock.dollars = 150 >>> stock.numShares 5.0 >>> stock.dollars = 600 >>> stock.dollars 600.0 >>> stock.numShares 20.0 >>> stock.deposit(60) >>> stock.dollars 660.0 >>> stock.numShares 22.0 """ def __init__(self, numShares, pricePerShare): self.numShares = numShares self.pricePerShare = pricePerShare @property def dollars(self): return self.numShares * self.pricePerShare @dollars.setter def dollars(self, x): self.numShares = x / self.pricePerShare if __name__ == "__main__": import doctest doctest.testmod()
なんか、普通に書いたら普通に出来ました。
Selfほど短くはなりませんでしたが・・・。
いまいち、このテストの価値がわかりません。