首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为当前类的任意对象调用itcl::scope?

如何为当前类的任意对象调用itcl::scope?
EN

Stack Overflow用户
提问于 2012-01-08 06:57:31
回答 1查看 397关注 0票数 0

itcl::scope返回$this的指定成员变量的全名。

如何为同一类(不是$this)的另一个对象调用itcl::scope

这里有一个解决方法。

代码语言:javascript
复制
itcl::class dummy {
    variable m_data

    method function { other } {
        if { [itcl::is object -class dummy $other] } {
            error "Invalid argument."
        }

        set name "@itcl $other [$other info variable m_data -name]"
        # OR
        set name [lreplace [itcl::scope m_data] 1 1 $other]

        puts "==== this is the name of m_data of of object $other: $name"
    }
}

但说得客气一点,这是丑陋的。

我认为$other info variable m_data -name应该返回我想要的内容,但它只是省略了对象的上下文。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-10 06:54:55

没有一种很好的方法来获取这些信息,也不一定是一件好事。通常,最好将对象的变量视为该对象实现的一部分,而不是其接口的一部分。因此,外部代码不应该像那样探查内部;如果对象外部的某些东西(包括类的其他成员)访问变量很重要,那么可以编写一个方法来返回对变量的引用(就像用itcl::scope生成的那样)。

代码语言:javascript
复制
itcl::class dummy {
    variable m_data

    protected method dataRef {} { itcl::scope m_data }

    method function { other } {
        if { [itcl::is object -class dummy $other] } {
            error "Invalid argument."
        }

        set name [$other dataRef]

        puts "==== this is the name of m_data of of object $other: $name"
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8773811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档