tcl - Tcl:添加实例类的不同方式之间的区别
问题描述
我找到了两种实例类的方法:
一个是:
class_name create instance
instance class_method
另一个是:
set instance [class_name new]
$instance class_method
每种方式都运作良好,那么两种方式之间有什么区别吗?
解决方案
我尝试过一些案例,
类定义如下:
oo::class create class_name {
variable text
method add {t} { set text $t }
method print { } { puts $text }
}
第一种方式:
class_name create foo
foo add "abc"
foo print
class_name create foo
它将返回:
abc
Error: can't create object "foo": command already exists with that name
第二种方式:
set foo [class_name new]
$foo add "abc"
$foo print
set foo [class_name new]
$foo add "def"
$foo print
它将返回:
abc
def
这是我发现的一个区别,似乎第二种方式更方便。
还是期待师父提供权威的答案或文件。
推荐阅读
- javascript - 检查数组是否包含给定数字
- angular - Angular 通用升级 8 -> 9 SSR 构建。走下一个错误的兔子洞
- javascript - 从在线 Power BI 仪表板下载数据
- python - Pandas 根据其他列中的条件和值创建新列
- css - 是否有内置方法可以使 Angular 对话框高于中心?
- python - 创建表并将数据流式传输到 db 文件后,无法在 sqlite 中找到我的表
- swift - 为什么蒸汽云部署失败?
- php - 从 Json 字符串中获取价值
- laravel - laravel 7 - 无法将输入参数传递给表单操作以进行搜索
- soapui - SOAP-UI:可以在扩展中增加一个属性吗?