itcl::scope返回$this的指定成员变量的全名。
如何为同一类(不是$this)的另一个对象调用itcl::scope?
这里有一个解决方法。
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应该返回我想要的内容,但它只是省略了对象的上下文。
发布于 2012-01-10 06:54:55
没有一种很好的方法来获取这些信息,也不一定是一件好事。通常,最好将对象的变量视为该对象实现的一部分,而不是其接口的一部分。因此,外部代码不应该像那样探查内部;如果对象外部的某些东西(包括类的其他成员)访问变量很重要,那么可以编写一个方法来返回对变量的引用(就像用itcl::scope生成的那样)。
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"
}
}https://stackoverflow.com/questions/8773811
复制相似问题