OTCL的多继承

时间:2025-02-17 14:33:44
 Class Thing
Class Animal
Class Other -superclass {Animal Thing} Thing instproc init {args} {
puts "here is init from:thing"
eval $self next $args
}
Thing instproc move {} {
puts "here is move from:thing"
} Animal instproc init {args} {
puts "here is init from:animal"
eval $self next $args
}
Animal instproc move {} {
puts "here is move from:animal"
eval $self next
} Other instproc init {args} {
puts "here is init from:other"
eval $self next $args
}
Other instproc move {} {
puts "here is move from:other"
eval $self next
}
Other other
other move

输出:

here is init from:other
here is init from:animal
here is init from:thing
here is move from:other
here is move from:animal
here is move from:thing

上面是简单地继承两个父类,在调用next的时候,顺序是按继承时候的从左到右的顺序,依次调用。

如果是这样:

Class theOne
Class Thing -superclass theOne
Class Animal
Class Other -superclass {Thing Animal} theOne instproc init {args} {
puts "here is init from:theOne"
eval $self next $args
}
theOne instproc move {} {
puts "here is move from:theOne"
eval $self next
} Thing instproc init {args} {
puts "here is init from:thing"
eval $self next $args
}
Thing instproc move {} {
puts "here is move from:thing"
eval $self next
} Animal instproc init {args} {
puts "here is init from:animal"
eval $self next $args
}
Animal instproc move {} {
puts "here is move from:animal"
eval $self next
} Other instproc init {args} {
puts "here is init from:other"
eval $self next $args
}
Other instproc move {} {
puts "here is move from:other"
eval $self next
}
Other other
other move

输出:

here is init from:other
here is init from:thing
here is init from:theOne
here is init from:animal
here is move from:other
here is move from:thing
here is move from:theOne
here is move from:animal