【问题标题】: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 的实例,它只显示firstVariablesecondVariable 而不是thirdVariable

    非常好的问题!

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

    addInstancesVariable: elements
        self object class instVarNames do: [:name |
            elements add: (InstanceVariableEyeElement host: self object instVarName: name) ]
    

    【讨论】:

      【解决方案2】:

      您可能希望在 Moose 中使用可自定义的 tools。在那里您可以非常轻松地添加/更改视图。在humane assessment blog 你可以找到例子

      【讨论】:

        猜你喜欢
        • 2011-12-09
        • 1970-01-01
        • 1970-01-01
        • 2013-08-18
        • 2011-01-07
        • 1970-01-01
        • 2013-06-15
        • 1970-01-01
        相关资源
        最近更新 更多