Code:
(码:)
package require TclOO
oo::class create Supe {
variable cape boots
constructor {} {
puts -nonewline "Supe: "
puts [info class variables [self class]]
}
}
oo::class create Clark {
superclass Supe
variable glasses suit
constructor {} {
puts -nonewline "Clark: "
puts [info class variables [self class]]
next
}
}
set hero [Clark new]
Output:
(输出:)
Clark: glasses suit
Supe: cape boots
Is it possible to get a list of Clark's member variables from within Supe's constructor without passing them into Supe as an argument?
(是否可以从Supe的构造函数中获取Clark的成员变量列表,而无需将其作为参数传递给Supe?)
Ultimately, the goal is to dynamically set derived class variables from a dict argument:
(最终,目标是从dict参数动态设置派生类变量:)
foreach {varName} [info class variables [self class]] {
variable $varName [dict get $args $varName]
}
If the above code can be used in the superclass constructor, it would avoid putting it in each derived class constructor.
(如果以上代码可在超类构造函数中使用,则应避免将其放在每个派生类构造函数中。)
ask by Brad Waite translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…