printOn: aStream
| normalized |
normalized := self normalized.
aStream nextPut: ${.
self isEmpty ifFalse: [
normalized printElem: 1 on: aStream.
2 to: self size do: [ :i |
aStream nextPutAll: ' . '.
normalized printElem: i on: aStream
].
].
aStream nextPut: $}
此 printOn:
方法有效,但检查器正在使用其他路径来打印数组。如何告诉 Inspector 对继承自 Array 的类使用上述方法?
最佳答案
Inspector 使用 gtDisplayOn:
来表示对象。
在Object
中,它的实现如下:
gtDisplayOn: stream
"This offers a means to customize how the object is shown in the inspector"
^ self printOn: stream
但是,Collection
将其重写为:
gtDisplayOn: stream
self printNameOn: stream.
stream
space;
nextPut: $[;
print: self size;
nextPutAll: (' item' asPluralBasedOn: self size);
nextPut: $];
space.
self size <= self gtCollectionSizeThreshold
ifTrue: [ self printElementsOn: stream ]
只需在类中再次重写它即可像 Object
一样使用 printOn:
。
关于pharo - 如何覆盖数组的打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55023421/