有没有办法确定 smalltalk 中已初始化变量的数据类型?
例如:
|abc|
abc := #(1 2 3 4 5 6) 'This is the array declared'
(abc isKindOf: Array) ifTrue: [ 'Check the data type of abc against array datatype'
^'Success!'
]
请求的原因:只有当它被特定数据类型调用时,我才需要实现 a 方法。
有没有办法确定 smalltalk 中已初始化变量的数据类型?
例如:
|abc|
abc := #(1 2 3 4 5 6) 'This is the array declared'
(abc isKindOf: Array) ifTrue: [ 'Check the data type of abc against array datatype'
^'Success!'
]
请求的原因:只有当它被特定数据类型调用时,我才需要实现 a 方法。
如果我正确理解您的问题,您可以使用
abc isMemberOf: Array
或者
abc class == Array
这会检查是否abc
是Array
类的一个实例(你称之为数据类型的东西)。
也可能
abc respondsTo: #message
可能对您有用,因为它检查是否为abc
.
Smalltalk 中也有一个常见的成语。定义返回falseisArray
的方法;在返回 true的类上Object
定义。像这样,您可以发送到任何对象。但是,如上所述,这个习语再次强调了次优设计。isArray
Array
#isArray
只需将该方法添加到数据类型 Array。即到类数组。
然后只有一个 Array 的实例才能调用它。