gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:03:05 27 4
gpt4 key购买 nike

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com