gpt4 book ai didi

date - Date dayMonthYearDo : look like in Smalltalk (Pharo/Squeak) 的正确参数是什么

转载 作者:行者123 更新时间:2023-12-01 09:20:46 25 4
gpt4 key购买 nike

Date dayMonthYearDo: aBlock 
"Supply integers for day, month and year to aBlock and return the result"

^ start dayMonthYearDo: aBlock

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

最佳答案

在这种情况下,注释“提供整数等”。表示参数 aBlock将接收三个整数作为“实际”参数:day numbermonth indexyear。这意味着您必须创建一个带有三个“正式”参数的 block ,例如 day。 , monthIndexyear如:

aDate dayMonthYearDo: [:day :monthIndex :year | <your code here>]

你在<your code here>里面写的代码可以引用“正式”参数day , monthIndexyear ,就好像它是一个带有这三个参数的方法一样。

这就是 Smalltalk 中 block 的一般工作方式。

示例

aDate
dayMonthYearDo: [:day :monthIndex :year |
monthIndex + day = 2
ifTrue: [Transcript show: 'Happy ' , year asString, '!']]

更新

上面的示例通过“巧妙地”比较 monthIndex + day 来检查 1 月 1 日。与 2 .事实上,由于两个变量都 >= 1,因此获得 2 的唯一方法是是什么时候daymonthIndex都是1 ,即当接收者aDate是 1 月 1 日。更“严肃”的方法看起来像

(monthIndex = 1 and: [day = 1]) ifTrue: [ <etc> ]

关于date - Date dayMonthYearDo : look like in Smalltalk (Pharo/Squeak) 的正确参数是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33397011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com