根据 ITcl 手册页,可以使用delegate method methodName to componentName ?as targetName?
( https://www.tcl.tk/man/tcl/ItclCmd/itcldelegate.html#M6 ) 声明委托方法。
在下面的代码中,Dog
该类有两个变量tail
,tail2
它们被初始化为一个Tail
对象。然而,只有wag2
方法保留了正确的mood
.
我想澄清一下委托是否将组件声明tail
为静态变量?
(仅供参考,这是对如何在 TCL 中使用 itcl::delegation 的评论的后续问题?)。
package require itcl
itcl::extendedclass Tail {
variable mood
constructor {_mood} {
set mood $_mood
}
method wag {} {
return "Wag, wag, wag $mood"
}
}
itcl::extendedclass Dog {
delegate method wag to tail
variable tail2
constructor {mood} {
set tail [namespace which [Tail #auto $mood]] ;#// https://stackoverflow.com/questions/68336192/how-to-use-itcldelegation-in-tcl
set tail2 [Tail #auto $mood]
}
method wag2 {} {
return [$tail2 wag]
}
}
puts [info patchlevel]
puts $itcl::patchLevel
Dog happydog :)
Dog saddog :(
puts [happydog wag]
puts [saddog wag]
puts [happydog wag2]
puts [saddog wag2]
预期的:
Wag, wag, wag :)
Wag, wag, wag :(
Wag, wag, wag :)
Wag, wag, wag :(
实际的:
Wag, wag, wag :( <==== note: happydog is not happy
Wag, wag, wag :(
Wag, wag, wag :)
Wag, wag, wag :(
输出截图(仅显示wag2
调用保持正确mood
):