Eua*_*n M 3 smalltalk date squeak pharo
Date dayMonthYearDo: aBlock
"Supply integers for day, month and year to aBlock and return the result"
^ start dayMonthYearDo: aBlock
Run Code Online (Sandbox Code Playgroud)
此消息的典型有效块应该是什么样的?
在这种情况下,评论"供应整数等" 意味着参数aBlock将接收三个整数作为"实际"参数:日期编号,月份索引和年份.这意味着,你必须创建一个块有三个"正式"的争论,比如说day,monthIndex和year在:
aDate dayMonthYearDo: [:day :monthIndex :year | <your code here>]
Run Code Online (Sandbox Code Playgroud)
你写的代码里面<your code here>可以参照"正式"的参数day,monthIndex并且year,就像如果有人用这三个参数的方法.
这就是块通常在Smalltalk中工作的方式.
例
aDate
dayMonthYearDo: [:day :monthIndex :year |
monthIndex + day = 2
ifTrue: [Transcript show: 'Happy ' , year asString, '!']]
Run Code Online (Sandbox Code Playgroud)
UPDATE
上述的"巧妙"比较检查1月1日的例子monthIndex + day有2.事实上,因为这两个变量> = 1来获得的唯一办法2是,当day和monthIndex都1,也就是说,当接收器aDate是1月1日更"严重"的做法看起来像
(monthIndex = 1 and: [day = 1]) ifTrue: [ <etc> ]
Run Code Online (Sandbox Code Playgroud)