oop - [incr Tcl] 中的静态函数继承

在 incr Tcl 中的继承没有按预期工作。考虑下面的代码。

package require Itcl

::itcl::class Base \
{
public {
    proc function { } { puts "==== Base::function" }
}
}

::itcl::class Derived { inherit Base }

Base::function
Derived::function    ;# FAILS

最后一行失败了,所以 Base::function 没有从 Derived 继承,尽管 Derived 继承自 Base.

我是做错了什么,还是incr Tcl被设计成这样?

最佳答案

阅读文档我不认为 itcl 类中的 proc 以您认为应该的方式工作:

proc name ?args? ?body? Declares a proc called name. A proc is an ordinary procedure within the class namespace. Unlike a method, a proc is invoked without referring to a specific object. When the proc body is executed, it will have automatic access only to common data members. If the args list is specified, it establishes the usage information for this proc. The body command can be used to redefine the proc body, but the args list must match this specification. Within the body of another class method or proc, a proc can be invoked like any other command-simply by using its name. In any other namespace context, the proc is invoked using a qualified name like "className::proc". Procs in a base class that are redefined in the current class, or hidden by another base class, can also be accessed via their qualified name.

我对此的解读是,proc 与它的类相关联,它可以在派生类中引用,但未在派生类中定义。例如以下作品:

package require Itcl

::itcl::class Base {
    public {
        proc function { } { puts "==== Base::function" }
    }
}

::itcl::class Derived { 
inherit Base 
    public {

        proc function { } { 
            puts "==== Derived::function"
            return [Base::function] 
        }
    }
}

Base::function
Derived::function    ;# FAILS

https://stackoverflow.com/questions/4756769/

相关文章:

recursion - 对于每个递归算法都可以创建等效的非递归版本吗?

.net - 以整数开头的标识符

teamcity - 是否可以在团队城市 build 中添加自由文本注释?

.net - 如何从带有procdump(或类似文件)的.Net应用程序中捕获未处理的异常?

.net - 如何拦截自定义处理的 WPF 绑定(bind)

maven - 有没有办法在 Maven 中包含同一 Artifact 的两个版本?

jsf-2 -

内的 Primefaces 条件逻辑

security - 编写自动更新客户端时我必须担心哪些安全问题?

boost-preprocessor - Boost.Preprocessor 是独立的吗?

task - Guice:提供程序中的注入(inject)器