Date dayMonthYearDo:的正确参数在Smalltalk(Pharo/Squeak)中看起来像什么
此消息的典型有效块应该是什么样子?类似于以下内容:Date dayMonthYearDo:的正确参数在Smalltalk(Pharo/Squeak)中看起来像什么,date,smalltalk,pharo,squeak,Date,Smalltalk,Pharo,Squeak,此消息的典型有效块应该是什么样子?类似于以下内容: Date dayMonthYearDo: aBlock "Supply integers for day, month and year to aBlock and return the result" ^ start dayMonthYearDo: aBlock 但是当然,你可以做不同的事情,只在文本上显示内容。在本例中,注释“提供整数等”意味着参数aBlock将收到三个整数作为“实际”参数:日数、月指数和年。这意味着您必须创建一个包含
Date dayMonthYearDo: aBlock
"Supply integers for day, month and year to aBlock and return the result"
^ start dayMonthYearDo: aBlock
但是当然,你可以做不同的事情,只在文本上显示内容。在本例中,注释“提供整数等”意味着参数aBlock
将收到三个整数作为“实际”参数:日数、月指数和年。这意味着您必须创建一个包含三个“正式”参数的块,例如,day
、monthIndex
和year
,如下所示:
Date today dayMonthYearDo: [:d :m :y| Transcript cr;
show: 'today is the ';
show: d;
show: 'th'
]
today is the 28th
更新
上面的例子通过“巧妙地”将monthIndex+day
与2
进行比较来检查1月1日。事实上,由于这两个变量都>=1,因此获得2
的唯一方法是当day
和monthIndex
都是1
时,即当接收者aDate
是1月1日时。一个更“严肃”的方法看起来像
aDate
dayMonthYearDo: [:day :monthIndex :year |
monthIndex + day = 2
ifTrue: [Transcript show: 'Happy ' , year asString, '!']]
(monthIndex=1和:[day=1])如果真的是:[]
但是请不要用monthIndex+day=2
来检查是否是1月1日:)@mgarciaisaia那是我的笑话!好的,我从一个问题开始——现在我有(两)个正确答案,我现在有3个问题。很明显,我误解了预定义日期>>dayMonthYearDo:method的目的。所以我的新问题是:1)Date>>dayMonthYearDo:仅仅是某个将来要定义的方法的占位符,它实际上是在块中定义的?我现在意识到,这里的答案是“是的”。2) 这里,对象“aDate”如何提供块中使用的day-monthIndex和year值?自动的?接收者是Date类的一个实例,这是在转移注意力吗?@EuanM请创建一个(或两个)新问题,而不是使用注释。“这将有助于做出更多的贡献。”莱安德罗已经完成了问题3。但上面的问题2)是一个关于您作为答案发布的代码工作原理的问题。当然正确的做法是在这个帖子中提问?谢谢你的正确答案。我一直希望实现的是找到日期或DateAndTime上预先存在的方法,该方法允许我更改日期、月份和年份以设置值,例如07 11 1984我将提出一个新问题。
aDate
dayMonthYearDo: [:day :monthIndex :year |
monthIndex + day = 2
ifTrue: [Transcript show: 'Happy ' , year asString, '!']]
(monthIndex = 1 and: [day = 1]) ifTrue: [ <etc> ]