2

有时我正在检查或探索包含许多实例变量的域对象。我想排除所有这些,除了我正在探索的实例的当前特定实例。

假设我正在使用实例变量检查 MyObject:文本、大小、状态。

  • 家属
  • 所有者
  • 窗户
  • 播音员
  • ...(很多 iv)
  • 文本
  • 尺寸
  • 地位

我想查看:

  • 文本
  • 尺寸
  • 地位

我看到你可以定义方法inspectorClass,但是EyeInspector 或EyeExplorer 是为了配置这种类型的视图而设计的吗?我应该继承 SelfEyeElement 类吗?

4

2 回答 2

0

我刚刚尝试并想出了这个:

想象一下这是你的课:

Object subclass: #MyClass
    instanceVariableNames: 'firstVariable secondVariable thirdVariable'
    classVariableNames: ''
    category: 'MyCategory'

然后制作以下检查员类:

EyeInspector subclass: #MyClassInspector
    instanceVariableNames: ''
    classVariableNames: ''
    category: 'MyCategory'

将以下方法添加到MyClass

inspectorClass
    ^ MyClassInspector

#addInstanceVariable:并覆盖MyClassInspector

addInstancesVariable: elements
    elements add: (InstanceVariableEyeElement host: self object instVarName: #firstVariable).
    elements add: (InstanceVariableEyeElement host: self object instVarName: #secondVariable)

检查 and 的一个实例,MyClass它只显示firstVariableandsecondVariable但不显示thirdVariable

MyClassInspector

非常好的问题!

更新:如果你想要一个检查器,它通常只显示在检查对象的类中指定的实例变量,而不是在检查对象的超类中,你可以#addInstanceVariable:在检查器类中使用它:

addInstancesVariable: elements
    self object class instVarNames do: [:name |
        elements add: (InstanceVariableEyeElement host: self object instVarName: name) ]
于 2014-07-02T17:20:07.730 回答
0

您可能希望使用 Moose 中的可定制工具。在那里,您可以非常轻松地添加/更改视图。在人性化评估博客中,您可以找到示例

于 2014-07-16T14:28:59.490 回答