例字频统计对比程序 Pascal Smalltalk PROGRAM Frequency N无消息模式方法,宜写算法” CONST Size=80: VARs: stringls1ze」; s cfk k, i: Integer “定义了四个临时变量” f: ARRAYL1. 26] OF Integer; f← Array new:26. BEGIN f是 Arrey实例长度26″ Writeln(enter line/) s← Prompter ReadIn(s) prompt: interline′ FOR i: =ITo 26 DO default “S是 Prompter的实例,装输入字串” FOR i: =1 To size do to:26 do:[:If at: I put: 0]. BEGIN 1 to: size do: [ I c: =aslowerCase(slil asLower Case if isLetter (c)THEN c isLetter ifTrue: L BEGIN k← c asciivalue k: -ord(c)-ord(/)+1 Sa asciivalue +1 f[k]:=f[k]+1 f at: k put: (f at: k)+1 END END FOR i: =1 To 26 DO Write(f[i],“)
例 字频统计对比程序 Pascal Smalltalk PROGRAM Frequency “无消息模式方法,宜写算法” CONST Size=80; VAR s:string[size]; | s c f k | k,i:Integer; “定义了四个临时变量” c:Char; f:ARRAY[1..26] OF Integer; f←Array new:26. BEGIN “f是Arrey实例长度26” Writeln(‘enter line’); s←Prompter ReadIn(s); prompt:‘enterline’ FOR i:=1TO 26 DO default: ‘’. f[i]:=0; “S是Prompter的实例,装输入字串” FOR i:=1 To size DO 1 to:26 do:[:I|f at:I put:0]. BEGIN 1 to:size do:[:I| c:=aslowerCase(s[i]); c←(s at:i) asLowerCase. if isLetter (c) THEN c isLetter ifTrue:[ BEGIN k←c asciiValue k:=ord(c)-ord(‘a’)+1; -$a asciiValue + 1. f[k]:=f[k]+1 f at:k put:(f at:k) + 1 END ] END; ]. FOR i:=1 To 26 DO ↑f Write(f[i], ‘’) END
4152类协议 类 名 标识符 超类名 标识符 单继承,只一个 实例变量名 标识符表 用于实例对象 类变量名 标识符表 用于类对象 汇集变量名 标识符 充作若干类共享的汇聚字 类方法 用于创建实例,并初始化 方法1 方法2 如上例 Array中的方法new 方法n 实例方法 刻画实例对象行为 方法 如上例中 asLowerCase, 方法2 at:put:, isLetter是对象 f,c的方法 方法n
4.1.5.2 类协议 类 名 超 类 名 实例变量名 类变量名 汇集变量名 标识符 标识符 标识符表 标识符表 标识符 类方法: 方法 1 方法 2 . . 方法 n 实例方法 : 方法 1 方法 2 . . 方法 n 单继承,只一个 用于实例对象 用于类对象 充作若干类共享的汇聚字 典 用于创建实例,并初始化 如上例 Array 中的方法 new: 刻画实例对象行为 如上例中 asLowerCase, at:put:, isLetter 是对象 s,f,c 的方法
Smalltalk程序- ob ]ec d重设计 A派生 直用
object d 重设计 c 直用 di1 ci1 ai1 ai2 di2 ci2 ci3 Smalltalk 程序 A 派生 a
4153一个完整的 Smalltalk程序 家庭财务帐目 建立全部流水帐类,直接挂在 Object上 class name Financialhistory superclass Object instance variable names caseOnHand incomes expenditures category Financial tools lass method initialBalance; amount “建立流水帐本初始为 amount(元)” f super new setinitialBalance: amounT new “建立流水帐本初始为0(元)” f super new setinitialBalance: 0 instance method receive: amount from: source incomes at: source put Iself total ReceivedFrom: source)+amount. 从来源 Source接收到的钱数,因而手头现金增加” cashOnHand+cashOn Hand amount incomes changed spend: amount for: reason “为事由 reason支付的钱数,因而手头现金减少。” expenditures at: reason put:(self totalSpentFor: reason)+ amount
4.1.5.3 一个完整的Smalltalk程序 家庭财务帐目 建立全部流水帐类,直接挂在Object上 class name FinancialHistory superclass Object instance variable names 'caseOnHand incomes expenditures' category 'Financial Tools' class method initialBalance:amount| | “建立流水帐本初始为amount(元)” ↑super new setinitialBalance:amounT new | | “建立流水帐本初始为0(元)” ↑super new setinitialBalance:0 instance method receive:amount from:source | | incomes at:source put:lself total ReceivedFrom:source)+amount. “从来源source接收到的钱数,因而手头现金增加”. cashOnHand←cashOnHand + amount. incomes changed spend:amount for:reason | | “为事由reason支付的钱数,因而手头现金减少。” expenditures at:reason put:(self totalSpentFor:reason) + amount
cashOnhand+ cashOnHand-amount expenditures changed CashOn hand “回答当前手头现金” cashOnHand expenditures “回答支出细目” ↑ expenditures Incomes “回答收入细目” Income s totalreceivefrom: source “回答自 Source收钱总数” (incomes includesKey: source ifTrue:[t incomes at: source iffalse:[↑0 totalspent For: reason “回答在 reason项上总支出 expenditures includesKey: reason) fTrue:[ t expenditures at: reason] iffalse:[↑0] private Setlnitialbalance: amount “实例变量初始化” cashOnhand← amount incomes+Dictionary new expenditures+Dictionary new
cashOnHand← cashOnHand - amount. expenditures changed CashOnHand| | “回答当前手头现金” ↑ cashOnHand expenditures | | “回答支出细目” ↑ expenditures incomes | | “回答收入细目” ↑ incomes totalReceiveFrom:source | | “回答自source收钱总数” (incomes includesKey:source) ifTrue:[↑incomes at:source] ifFalse:[↑0] totalSpentFor:reason | | “回答在reason项上总支出” (expenditures includesKey:reason) ifTrue:[↑expenditures at:reason] ifFalse:[↑0] private SetlnitialBalance:amount| | “实例变量初始化” cashOnHand←amount. incomes←Dictionary new. expenditures←Dictionary new