我正在嘗試通過Dolphin Smalltalk X6附帶的教程學習Smalltalk。「:=」和Smalltalk中的二進制消息優先級
我的問題涉及評估帶有多條消息的表達式。
我的理解是,首先評估簡單的消息,然後評估二進制消息,最後評估關鍵字消息(除了括號中的代碼)。但是,在下面的示例中(第Dolphin Smalltalk tutorial中發現),我無法將此理解應用於第二行。
playground := Playground new.
teresa := playground add: Triangle new. "Misunderstood code"
teresa class. "Evaluates to 'Triangle'"
如果我的理解是正確的,2號線將正是如此評價:
1. Simple message 'new' sent to Triangle, triangle object as response
2. Binary message ':=' with parameter 'playground' sent to 'teresa'.
3. Keyword message 'add:' with parameter 'triangle object' sent to 'teresa'.
4. teresa class. "evaluates to 'Playground'".
我的誤解是如何「鄧麗君」來指代匿名三角對象,而不是遊樂場對象簡稱到'遊樂場'。
我已經查閱了關於Smalltalk評估的第二個解釋,提及:=或添加:是沒有成功的特殊情況,我能想到的唯一的其他解釋是一個根本性的誤解。
任何幫助矯直我?
讓人想知道爲什麼:=不是Smalltalk中的消息。爲什麼不讓變量接收:=? – Hendekagon 2013-03-11 05:50:11
因爲變量實際上是指向對象的指針,所以它不是對象本身。消息發送給變量指向的對象,而:=運算符則更改變量指向的對象。 – 2013-03-12 22:55:02
hmm對象指針不是對象,所以它們不能接收:=(或pointTo:anObject或→) – Hendekagon 2013-04-18 00:20:37