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

标签 oop inheritance tcl static-members 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

关于oop - [incr Tcl] 中的静态函数继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4756769/

相关文章:

JAVA - 对象构造函数不接受参数?

javascript - 如何在不使用全局作用域的情况下将 javascript 分离到文件中?

c++ - C++编译器如何解析虚函数访问非虚数据成员

tcl - 在 tcl/tk 子窗口中,我无法为我的条目小部件设置默认值

tcl - 如何从另一个列表中减去一个列表并检查模式是否按顺序排列?

javascript - Mootools类创建

c++ - 我如何在其他类(或任何类)中使用抽象类?

javascript __proto__ 不会产生与 "prototype"继承相同的效果

c# - 向下转换泛型参数时如何修复转换错误

arguments - 为什么 Tcl 允许过程名称带空格,但参数不允许带空格?