如何覆盖数组的打印?
How do I override the printing of arrays?
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 使用其他途径打印数组。我如何告诉 Inspector 对我从 Array 继承的 class 使用上述方法?
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 ]
只需在您的 class 中再次覆盖它即可像 Object
那样使用 printOn:
。
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 使用其他途径打印数组。我如何告诉 Inspector 对我从 Array 继承的 class 使用上述方法?
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 ]
只需在您的 class 中再次覆盖它即可像 Object
那样使用 printOn:
。