--sumimself@(Cloneable traits) klone [| klone dummy | klone: Cloneable clone. dummy: self clone. self slotNames do: [| :each | (self delegateNames includes: each) not ifTrue: [ klone addSlot: each valued: (dummy removeSlotNamed: each)]]. klone addDelegate: #traits valued: self]. lobby addSlot: #bankAccount valued: Cloneable clone. "lobby は省略可" bankAccount addSlot: #dollars valued: 0. self@bankAccount deposit: x@(Number traits) [self dollars: (self dollars + x)]. "メソッドは総称関数的に定義" "@ はパラメータ変数の型指定。ちなみに Slate では a Point 生成にはカンマを使う" self@bankAccount withdraw: x@(Number traits) [self dollars: ((self dollars - x) max: 0)]. bankAccount dollars: 200. => 200. bankAccount deposit: 50. => 250 bankAccount withdraw: 100. => 150 bankAccount withdraw: 200. => 0 addSlot: #myAccount valued: bankAccount klone. "bankAccount をペアレント(aka プロトタイプ)とするチャイルドの作成" myAccount dollars. => 0 "プロトタイプの同名スロット値と同じ値を持つ" myAccount dollars: 500. => 500 bankAccount dollars. => 0 "プロトタイプの同名スロット値には影響が及ばない" addSlot: #stockAccount valued: bankAccount klone. stockAccount addSlot: #numShares valued: 0. stockAccount addSlot: #pricePerShare valued: 30. self@stockAccount dollars [self numShares * self pricePerShare]. self@stockAccount dollars: x@(Number traits) [self numShares: ((x as: Float) / self pricePerShare) truncated. self dollars]. stockAccount dollars: 300. => 300 stockAccount numShares. => 10 stockAccount dollars: 150. => 150 stockAccount numShares. => 5 addSlot: #myStock valued: stockAccount klone. myStock dollars: 600. => 600 myStock numShares. => 20 myStock deposit: 60. => 660 myStock numShares. => 33 myStock withdraw: 90. => 570 myStock numShares. => 19 self@bankAccount deposit: x@(Number traits) [self dollars: (self dollars + (x * 0.9))]. self@bankAccount withdraw: x@(Number traits) [self dollars: ((self dollars - (x * 0.9)) max: 0)]. "#deposit: 、#withdraw: の仕様を変えてみる" bankAccount dollars. => 0 bankAccount deposit: 100. => 90.0 myAccount dollars. => 500 myAccount withdraw: 100. "チャイルドたちにもペアレントに対する改変の影響が及ぶ" => 410.0 stockAccount dollars. => 150 stockAccount deposit: 100. => 240 myStock dollars. => 480.0
このページを編集 (2961 bytes)
以下の 2 ページから参照されています。 |
This page has been visited 2922 times.