开发者

Can't hide constructor at [incr Tcl]

Consider the code below开发者_如何学Go.

package require Itcl 

::itcl::class A \ 
{ 
private { 
    constructor { } { } { puts "==== at A::constructor" } 

    method f { } { puts "==== at A::f" } 
} 
} 

A a ;# PASSES

a f ;# fails

For class A the constructor is private, but it is still possible to define an object of A.

Am I doing something wrong, or incr Tcl is designed to behave so?


I believe this is the way itcl work, if you look at the specification for a class:

itcl::class className {
inherit baseClass ?baseClass...? 
constructor args ?init? body 
destructor body 
method name ?args? ?body? 
proc name ?args? ?body? 
variable varName ?init? ?config? 
common varName ?init? 
public command ?arg arg ...? 
protected command ?arg arg ...? 
private command ?arg arg ...? 
set varName ?value? 
array option ?arg arg ...? 
} 
className objName ?arg arg ...? 
objName method ?arg arg ...? 
className::proc ?arg arg ...? 

you can see that private/protected can be applied to command but not to the constrcutor or the destructor. Looking here at the documentation on the itcl design patterns may also give some clues as to how to achieve something close to a private constructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜