程序代写代做代考 Smalltalk Lecture 7

Smalltalk Lecture 7
1

Three kinds of variables associated with classes
Instance variable: each instance of the class and each instance of its subclasses gets its own copy
Class variable: only one copy, which is shared by all instances of the class and all instances of its subclasses
Class instance variable: one copy which is shared by all instances of the class, and each subclass gets its own copy which is shared by all instances of that subclass
2

Account class
Object subclass: #Account.
Account instanceVariableNames: ‘balance income outgo’. “instance variables”
Account addClassVarName: ‘Total’. “class variable”
Account class instanceVariableNames: ‘Subtotal’.
“class instance variable”
3

Account instance methods
Account extend [ open: b [
balance := b.
Total := Total + b.
self class addSubtotal: b. income := Dictionary new. outgo := Dictionary new
]
getBalance [ ^ balance ] ]
4

Account class methods
Account class extend [ new [
Total isNil ifTrue: [ Total := 0 ]. Subtotal isNil ifTrue: [ Subtotal := 0 ]. ^ super new
]
addSubtotal: b [ Subtotal := Subtotal + b ] getTotal [ ^ Total ]
getSubtotal [ ^ Subtotal ]
]
5

CheckingAccount subclass
Account subclass: #CheckingAccount.
CheckingAccount instanceVariableNames: ‘checksUsed checksLeft’.
CheckingAccount extend [ open: b [
super open: b. checksUsed := 0. checksLeft := 0
] ]
6

SavingsAccount subclass
Account subclass: #SavingsAccount.
SavingsAccount instanceVariableNames: ‘interestRate’.
SavingsAccount extend [ open: b [
super open: b.
interestRate := 0 ]
]
7

Client code – accessing instance vars
a1 := Account new open: 100.
a2 := Account new open: 200.
c1 := CheckingAccount new open: 400. c2 := CheckingAccount new open: 500. s1 := SavingsAccount new open: 700. s2 := SavingsAccount new open: 800.
a1 getBalance printNl. a2 getBalance printNl. c1 getBalance printNl. c2 getBalance printNl. s1 getBalance printNl. s2 getBalance printNl.
8
100
200
400
500
700
800

Accessing class vars and class instance vars
Account getTotal printNl.
Account getSubtotal printNl. CheckingAccount getTotal printNl. CheckingAccount getSubtotal printNl. SavingsAccount getTotal printNl. SavingsAccount getSubtotal printNl.
Recall:
 balance (instance var) – each instance has its own copy  Total (class var) – only one copy that is shared by all
 Subtotal (class instance var) – each subclass has its own copy, which is shared by all instances of that subclass
9
2700
300
2700
900
2700
1500

More Account instance methods
Account extend [
deposit: amount from: source [
balance := balance + amount. income at: source
put: (amount + (self totalReceivedFrom: source)). Total := Total + amount.
self class addSubtotal: amount ]
totalReceivedFrom: source [
^ (income includesKey: source)
ifTrue: [ income at: source ]
ifFalse: [ 0 ] ]
]
10

Still more Account instance methods
Account extend [
withdraw: amount for: reason [
balance := balance – amount. outgo at: reason
put: (amount + (self totalSpentFor: reason)). Total := Total – amount.
self class addSubtotal: 0 – amount
]
totalSpentFor: reason [
^ (outgo includesKey: reason) ifTrue: [ outgo at: reason ] ifFalse: [ 0 ]
] ]
11

Account client
a1 deposit: 1000 from: ‘dad’.
a1 withdraw: 400 for: ‘car’.
a1 deposit: 800 from: ‘mom’.
a1 withdraw: 100 for: ‘party’.
a1 deposit: 700 from: ‘dad’.
a1 withdraw: 200 for: ‘car’.
Transcript display: ‘Total from dad = ‘;
display: (a1 totalReceivedFrom: ‘dad’); cr. Transcript display: ‘Total for car = ‘;
display: (a1 totalSpentFor: ‘car’); cr.
Total from dad = 1700
Total for car = 600
12

Yet another Account instance method
Account extend [ display [
Transcript display: ‘Balance = ‘; display: balance; cr ]
]
a1 display. a2 display.
Balance = 1900
Balance = 200
13

More CheckingAccount instance methods
CheckingAccount extend [ orderNewChecks: number [
checksLeft := checksLeft + number ]
writeCheck: amount for: reason [ checksUsed := checksUsed + 1. checksLeft := checksLeft – 1. self withdraw: amount for: reason
] ]
14

CheckingAccount client
c1 deposit: 3000 from: ‘inheritance’. c1 orderNewChecks: 100.
c1 writeCheck: 800 for: ‘vacation’. c1 writeCheck: 400 for: ‘vacation’.
15

Another CheckingAccount instance method
CheckingAccount extend [ display [
super display.
Transcript display: ‘Checks used = ‘;
display: checksUsed; cr. Transcript display: ‘Checks left = ‘;
] ]
c1 display. c2 display.
16
display: checksLeft; cr
Balance = 2200
Checks used = 2
Checks left = 98
Balance = 500
Checks used = 0
Checks left = 0

More SavingsAccount instance methods
SavingsAccount extend [ setRate: percentage [
interestRate := percentage ]
earnInterest [ | temp |
temp := (balance * interestRate / 100).
self deposit: temp from: ‘interest’ ]
]
17

SavingsAccount client
s1 setRate: 2.00.
s1 deposit: 2000 from: ‘paycheck’. s1 earnInterest.
s1 deposit: 300 from: ‘cash’.
s1 earnInterest.
18

Another SavingsAccount instance method
SavingsAccount extend [ display [
super display.
Transcript display: ‘Interest rate = ‘;
display: interestRate; cr. Transcript display: ‘Total interest = ‘;
] ]
s1 display. s2 display.
19
display: (self totalReceivedFrom: ‘interest’); cr
Balance = 3115.08
Interest rate = 2.0
Total interest = 115.08
Balance = 800
Interest rate = 0
Total interest = 0

Client code
a1 getBalance printNl. a2 getBalance printNl. c1 getBalance printNl. c2 getBalance printNl. s1 getBalance printNl. s2 getBalance printNl.
Account getTotal printNl.
Account getSubtotal printNl. CheckingAccount getTotal printNl. CheckingAccount getSubtotal printNl. SavingsAccount getTotal printNl. SavingsAccount getSubtotal printNl.
20
1900
200
2200
500
3115.08
800
8715.08
2100
8715.08
2700
8715.08
3915.08