Date dayMonthYearDo的正确参数是什么:在Smalltalk(Pharo/Squeak)中看起来像

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)

此消息的典型有效块应该是什么样的?

Lea*_*lia 5

在这种情况下,评论"供应整数等" 意味着参数aBlock将接收三个整数作为"实际"参数:日期编号,月份索引年份.这意味着,你必须创建一个块有三个"正式"的争论,比如说day,monthIndexyear在:

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 + day2.事实上,因为这两个变量> = 1来获得的唯一办法2是,当daymonthIndex1,也就是说,当接收器aDate是1月1日更"严重"的做法看起来像

(monthIndex = 1 and: [day = 1]) ifTrue: [ <etc> ]
Run Code Online (Sandbox Code Playgroud)

  • 但是_please_不要使用`monthIndex + day = 2`来检查它是否是1月1日:) (3认同)