vieweditattachhistoryswikistopchangessearchhelp

Self の CUI で BankAccount

--sumim
globals _AddSlotsIfAbsent: 
   (| 
      bankAccount <- ().
      account <- ().
      stockAccount <- ().
      stock <- ().
   |)
bankAccount:
   (|
      parent* = traits clonable.
      dollars <- 200.
      deposit: x = (dollars + x).
      withdraw: x = (dollars: (0 max: dollars - x)).
   |)
bankAccount dollars
==> 200
bankAccount deposit: 50
==> 250
bankAccount withdraw: 100
bankAccount dollars
==> 150
bankAccount withdraw: 200
bankAccount dollars
==> 0

account: bankAccount copy
account deposit: 500
account dollars
==> 500
bankAccout dollars
==> 0   # プロトタイプのスロットには影響なし。

stockAccount: bankAccount copy
stockAccount _AddSlots: 
   (|
      numShares <- 10.
      pricePerShare <- 30.
      dollars = (numShares * pricePerShare).
      dollars: x = (numShares: x / pricePerShare).
   |)
stockAccount dollars
==> 300
stockAccount dollars: 150
stockAccount dollars
==> 150
stockAccount numShares
==> 5   # 株数値が変更されている。

stock: stockAccount copy
stock dollars: 600
stock numShares
==> 20
stock deposit: 60
stock dollars
==> 660
stock numShares
==> 20

このページを編集 (1136 bytes)


Congratulations! 以下の 2 ページから参照されています。

This page has been visited 3537 times.