Smalltalk at: #sgetterGenerator put: "この行から" [ :class | class instVarNames do: [ :slot | {slot, String cr, String tab, '^ ', slot. slot, ': x', String cr, String tab, slot, ' := x'} do: [ :code | class compile: code classified: 'accessing']]]. "この行までが一文" Object subclass: #BankAccount "この行から" instanceVariableNames: 'dollars' classVariableNames: '' poolDictionaries: '' category: 'Category-BankAccount'. "この行までが一文" Utilities authorInitialsPerSe isEmptyOrNil ifTrue: [Utilities setAuthorInitials: 'your initial']. sgetterGenerator value: BankAccount. "この行から" #('deposit: x self dollars: self dollars + x' 'accessing' 'withdraw: x self dollars: (0 max: (dollars - x))' 'accessing' 'initialize dollars := 0' 'initialize') pairsDo: [ :code :category | BankAccount compile: code classified: category]. "この行までが一文" Smalltalk at: #account put: (BankAccount new initialize; yourself). account dollars: 200. account dollars "==> 200" account deposit: 50. account dollars. "==> 250" account withdraw: 100. account dollars. "==> 150" account withdraw: 200. account dollars. "==> 0" BankAccount subclass: #StockAccount "この行から" instanceVariableNames: 'numShares pricePerShare' classVariableNames: '' poolDictionaries: '' category: 'Category-BankAccount'. "この行までが一文" sgetterGenerator value: StockAccount. "この行から" #('dollars ^ self numShares * self pricePerShare' 'accessing' 'dollars: x self numShares: x asFloat / self pricePerShare' 'accessing' 'initialize numShares := 10. pricePerShare := 30' 'initialize') pairsDo: [ :code :category | StockAccount compile: code classified: category]. "この行までが一文" Smalltalk at: #stock put: (StockAccount new initialize; yourself). 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"
(FileStream oldFileNamed: 'UserScriptionHelping.cs') fileIntoNewChangeSet. #BankAccount << Object attribute: #dollars; define: 'initialize' as: 'dollars := 200'; define: 'deposit: x' as: 'self dollars: self dollars + x'; define: 'withdraw: x' as: 'self dollars: (0 max: self dollars - x)'. #account <== (BankAccount new initialize; yourself). account dollars. "==> 200" account deposit: 50. account dollars. "==> 250" account withdraw: 100. account dollars. "==> 150" account withdraw: 200. account dollars. "==> 0" #StockAccount << BankAccount attribute: #numShares; attribute: #pricePerShare; define: 'initialize' as: 'numShares := 10. pricePerShare := 30'; define: 'dollars' as: '^ self numShares * self pricePerShare'; define: 'dollars: x' as: 'self numShares: x asFloat / self pricePerShare'. #stock <== (StockAccount new initialize; yourself). stock numShares. "==> 10" stock dollars. "==> 300" stock dollars: 600. stock numShares. "==> 20.0" stock deposit: 60. stock dollars. "==> 660.0" stock numShares. "==> 22.0"
は、#BankAccount << Object の返値(BankAccount に束縛されたオブジェクト)に対して、#BankAccount << Object attribute: #dollars; define: 'initialize' as: 'dollars := 200'; define: 'deposit: x' as: 'self dollars: self dollars + x'; define: 'withdraw: x' as: 'self dollars: (0 max: self dollars - x)'.
という複数の文(式)を連続して一気に評価したのと同じ意味を持ち、#BankAccount << Object. BankAccount attribute: #dollars. BankAccount define: 'initialize' as: 'dollars := 200'. BankAccount define: 'deposit: x' as: 'self dollars: self dollars + x'. BankAccount define: 'withdraw: x' as: 'self dollars: (0 max: self dollars - x)'.
(FileStream oldFileNamed: 'PrototypeScripting.cs') fileIntoNewChangeSet. (#BankAccount asClone: Object) attribute: #dollars value: 200; define: 'deposit: x' as: 'self dollars: self dollars + x'; define: 'withdraw: x' as: 'self dollars: (0 max: self dollars - x)'. BankAccount dollars. "==> 200" BankAccount deposit: 50. BankAccount dollars. "==> 250" BankAccount withdraw: 100. BankAccount dollars. "==> 150" BankAccount withdraw: 200. BankAccount dollars. "==> 0" #MyAccount asClone: BankAccount. MyAccount deposit: 500. MyAccount dollars. "==> 500" BankAccount dollars. "==> 0 # プロトタイプのスロットには影響なし。" (#StockAccount asClone: BankAccount) attribute: #numShares value: 10; attribute: #pricePerShare value: 30; define: 'dollars' as: '^ self numShares * self pricePerShare'; define: 'dollars: x' as: 'self numShares: x asFloat / self pricePerShare'. StockAccount dollars. "==> 300" StockAccount dollars: 150. StockAccount dollars. "==> 150.0" StockAccount numShares. "==> 5.0 # 株数値が変更されている。" #MyStock asClone: StockAccount. MyStock dollars: 600. MyStock numShares. "==> 20.0" MyStock deposit: 60. MyStock dollars. "==> 660.0" MyStock numShares. "==> 22.0" MyStock withdraw: 120. MyStock dollars. "==> 540.0" MyStock numShares. "==> 18.0"
このページを編集 (11996 bytes)
以下の 4 ページから参照されています。 |
This page has been visited 5356 times.