将方法名称作为字符串给出时,如何调用Squeak方法?

Oha*_*had 1 smalltalk squeak

将方法名称作为字符串给出时,如何调用Squeak方法?可能吗 ?

Mar*_*inW 6

尝试:

perform: aSymbol
perform: aSymbol with: anObject
perform: aSymbol withArguments: anArrayOfArguments
Run Code Online (Sandbox Code Playgroud)

其中aSymbol是您的方法选择器.例如,而不是1 + 2你可以写:

1 perform: #+ with: 2
Run Code Online (Sandbox Code Playgroud)

如果您的方法选择器存储为a String,则必须将其转换为Symbol第一个:

1 perform: '+' asSymbol with: 2

5 perform: 'raisedTo:modulo:' asSymbol withArguments: #(2 3)
Run Code Online (Sandbox Code Playgroud)