【问题标题】:Hiding instance variables in the EyeInspector or EyeExplorer在 EyeInspector 或 EyeExplorer 中隐藏实例变量
【发布时间】:2014-07-02 11:59:08
【问题描述】:
有时我正在检查或探索包含许多实例变量的域对象。我想排除所有这些,除了我正在探索的当前特定实例。
假设我正在使用实例变量检查 MyObject:文本、大小、状态。
- 家属
- 所有者
- 窗口
- 播音员
- ...(大量静脉注射)
- 文字
- 尺寸
- 状态
我要查看:
我看到你可以定义方法inspectorClass,但是EyeInspector 或EyeExplorer 是为了配置这种类型的视图而设计的吗?
我应该继承 SelfEyeElement 类吗?
【问题讨论】:
标签:
reflection
smalltalk
pharo
inspector
【解决方案1】:
我刚刚尝试并想出了这个:
想象这是你的班级:
Object subclass: #MyClass
instanceVariableNames: 'firstVariable secondVariable thirdVariable'
classVariableNames: ''
category: 'MyCategory'
然后创建以下检查器类:
EyeInspector subclass: #MyClassInspector
instanceVariableNames: ''
classVariableNames: ''
category: 'MyCategory'
将以下class方法添加到MyClass:
inspectorClass
^ MyClassInspector
并在MyClassInspector 中覆盖#addInstanceVariable::
addInstancesVariable: elements
elements add: (InstanceVariableEyeElement host: self object instVarName: #firstVariable).
elements add: (InstanceVariableEyeElement host: self object instVarName: #secondVariable)
检查MyClass 的实例,它只显示firstVariable 和secondVariable 而不是thirdVariable:
非常好的问题!
更新:如果您想要一个检查器,通常只显示在检查对象的类中指定的实例变量,而不是在检查对象的超类中,您可以在检查器类中使用此 #addInstanceVariable::
addInstancesVariable: elements
self object class instVarNames do: [:name |
elements add: (InstanceVariableEyeElement host: self object instVarName: name) ]