在tcl9.0中使用oo::configurable

问题描述 投票:0回答:1

我正在使用新版本tcl9.0中的configure,它可以工作,但我有一个问题: 如何使类既可配置又抽象? 我尝试这样做:

oo::abstract create Simulator {
        mixin oo::configurable
        property Name -set {
            error "Not implemented"
        }
        variable Name
}

但是它会引发属性未知的错误。 预先感谢您!

properties tcl abstract
1个回答
0
投票

你需要做:

oo::configurable create Simulator {
    self mixin -append oo::abstract
    property Name -set {
        error "Not implemented"
    }
    variable Name
}

这是因为

oo::configurable
做了一些狡猾的事情,将
property
添加到配置命令集中,这意味着需要在调用
oo::define
之前对其进行断言(是的,类只是在其构造函数中将其传递出去);在通话期间添加它意味着只有下一次
oo::define
被调用才会受到影响。

相比之下,

oo::singleton
非常简单;它目前只是使
create
new
直接成为相关类(而不是其子类)上的非导出方法,并且不会更改定义命令上下文。这使得它足够简单,可以作为 mixin 工作。我们使用
self mixin -append
是因为我们可能向类中添加其他mixin,并且想要影响类对象本身。

© www.soinside.com 2019 - 2024. All rights reserved.