Tcl:从表列表继承

Tcl: Inherit from tablelist

如何从 tablelist 继承我自己的 class? 此代码不起作用(失败的命令 inherit

package require tablelist

::itcl::class myTableList {
    inherit ::tablelist
}

以及constructor应该怎么写?

P.S。 tablelist 版本为 4.8

谢谢。

Tablelist 不使用 [incr Tcl](或 Tcl 的任何其他对象系统),因此不能从中继承。

虽然您不能从 tablelist 继承,因为它不是可继承的 class,但您可以 包装它并在外部添加功能。在最简单的情况下——修改或添加方法——你可以通过委托来完成。方法如下(使用 TclOO):

### This metaclass makes doing the construction look like standard Tk.
oo::class create WidgetWrapper {
    # It's a metaclass, so it inherits from oo::class
    superclass oo::class

    method unknown {methodName args} {
        # See if we actually got a widget name; do the construction if so
        if {[string match .* $methodName]} {
            set widgetName $methodName

            # There's a few steps which can't be done inside the constructor but have to
            # be at the factory level.
            set obj [my new $widgetName {*}$args]
            rename $obj $widgetName

            # Tk widget factories *MUST* return the path name as the name of the widget.
            # It *MUST NOT* be colon-qualified, and it *MUST* refer to a widget.
            return $widgetName
        }

        # Don't know what's going on; pass off to standard error generation
        next $methodName {*}$args
    }
    unexport new create unknown
}

### This does the actual wrapping.
WidgetWrapper create WrappedTablelist {
    constructor {pathName args} {
        # Make the widget and *rename* it to a known name inside the object's
        # private namespace. This is magical and works.
        rename [tablelist::tablelist $pathName {*}$args] widget
    }

    # Delegate unknown method calls to the underlying widget; if they succeed,
    # bake the delegation in more permanently as a forward.
    method unknown {methodName args} {
        try {
            return [widget $methodName {*}$args]
        } on ok {} {
            oo::objdefine [self] forward $methodName widget $methodName
        }
    }
}

然后您可以从 WrappedTablelist 和 add/define 继承您想要的行为。使用unknown做委托初始化有点乱,但是tablelist有大量的方法所以在这里列出它们会有点痛苦。

您可能会在 itcl 中使用类似的方案,但我不太了解。