小智 8
究竟.当您发送消息#new时,它不仅会创建对象,还会发送#initialize信息.这使您可以自定义对象的初始化.看:
Behavior >> new
"Answer a new initialized instance of the receiver (which is a class) with no indexable variables. Fail if the class is indexable."
^ self basicNew initialize
Run Code Online (Sandbox Code Playgroud)
然后:
ProtoObject >> initialize
"Subclasses should redefine this method to perform initializations on instance creation"
Run Code Online (Sandbox Code Playgroud)
和:
Behaviour >> basicNew
"Primitive. Answer an instance of the receiver (which is a class) with no
indexable variables. Fail if the class is indexable. Essential. See Object
documentation whatIsAPrimitive."
<primitive: 70>
self isVariable ifTrue: [ ^ self basicNew: 0 ].
"space must be low"
OutOfMemory signal.
^ self basicNew "retry if user proceeds"
Run Code Online (Sandbox Code Playgroud)
所以... #basicNew是创建对象的原语.通常,你使用#new,如果你不想要任何特殊的东西,你就不会实现#initialize,因此会执行#ProtoObject的空实现.否则,您可以直接发送#basicNew,但您可能不应该这样做.
干杯
| 归档时间: |
|
| 查看次数: |
4591 次 |
| 最近记录: |