简体   繁体   English

Smalltalk x raiseTo:y错误

[英]Smalltalk x raisedTo: y error

Good day! 美好的一天! I Have my report tomorrow and i'm reviewing about Smalltalk. 我明天有报告,我正在审查Smalltalk。 I tried to use the raisedTo: method but it gives me this error: 我尝试使用raisedTo:方法,但它给了我这个错误:

 MessageNotUnderstood: Character>>raisedTo:

Here's my code: 这是我的代码:

|x y z|
x := UIManager default request: 'P1: '.
y := UIManager default request: 'P2: '.
z := x raisedTo: y.
self inform: z.

Try the following: 请尝试以下操作:

|x y z|
x := UIManager default request: 'P1: '.
y := UIManager default request: 'P2: '.
z := x asNumber raisedTo: y asNumber.
self inform: z asString.

Note how the selectors #asNumber and #asString convert objects to the proper types. 注意选择器#asNumber和#asString如何将对象转换为适当的类型。

Smalltalk is dynamically typed, but that does not mean you can pass any type of object to a method. Smalltalk是动态类型的,但这并不意味着您可以将任何类型的对象传递给方法。

Your code performs #raisedTo: on x. 您的代码在x上执行#raisedTo:。 However, x is a String, not a subclass of Number where #raisedTo: is implemented. 但是,x是一个字符串,而不是在其中实现#raisedTo:的Number的子类。 So you initial error is caused by String not understanding #raisedTo:. 因此,您最初的错误是由String无法理解#raisedTo:引起的。 (You can check where #raisedTo: is implemented by using the “Method finder” under the Tools menu.) I correct this by sending #asNumber to x. (您可以使用“工具”菜单下的“方法查找器”检查#raisedTo:在何处实现。)我通过将#asNumber发送给x来更正此问题。

Likewise, the argument you send to #raisedTo: must also be a number. 同样,您发送给#raisedTo:的参数也必须是数字。 Here the correction is the same; 这里的校正是相同的; send #asNumber to y. 将#asNumber发送给y。

Finally, #inform: expects a String, not a number. 最后,#inform:需要一个字符串,而不是一个数字。 The correction here is to send #asString to the number. 此处的更正是将#asString发送给该号码。

Note how #asString and #asNumber will not change the object you send the message to. 请注意,#asString和#asNumber将如何更改将消息发送到的对象。 Instead a new object of the appropriate type is answered. 相反,将回答适当类型的新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM