让怙我们有两个类,如下所示:TclOO:访问嵌套对象
oo::class create InsideThing {
constructor {} {
puts "Made a [self] that is an InsideThing"
}
destructor {
puts "Deleted a [self] that is an InsideThing"
}
method echo {text} {
puts "[self]: $text"
}
}
oo::class create Container {
constructor {} {
puts "Created a [self] that is a Container"
InsideThing create inner1
InsideThing create inner2
}
destructor {
puts "Deleted a [self] that is a Container"
}
method echo1 {text} {
# how to do something like this:
$inner1 echo $text
}
}
我怎么会去访问这些内置物?我想要做的事情如下:
set c [Container new]
# (1) accessing inner1 indirectly
$c echo1 "Hallo World"
# (2) accessing inner1 dirctly
$c inner1 echo "Hallo World"
有没有办法做到这一点?这种做法甚至有意义吗?
我想要实现的是一个嵌套的对象结构(基本上树状)。我希望能够通过调用节点上的方法(例如父节点,子节点)来导航此结构。也破坏了根应该销毁所有的孩子(这就是为什么我用create
创建父命名空间内嵌套的对象)
非常感谢。我期望它是一些简单的东西,非常有用,知道它是非常有用的;) – PeterE 2014-11-05 16:00:22
对于任何人一起阅读,我在某些地方使用'innerABC'而不是'inner1'来使它更清晰,名称与什么相匹配。就这样… – 2014-11-05 16:19:13