I was going through (using Squeak) the Discovering Better Code: Bowling for Smalltalk Series by Ron Jeffries and I can't get pass through the third article.
我正在经历(使用Squeak)Ron Jeffries的“发现更好的代码:Bowling for Smalltalk系列”,我无法通过第三篇文章。
A new class (called Frame) is being created which takes an array as an argument in the constructor.
正在创建一个新类(称为Frame),它将数组作为构造函数中的参数。
Frame class>>new: anArray
^self new setRolls: anArray
Frame>>setRolls: anArray
rolls := anArray
When I try to run this in a simple test:
当我尝试在一个简单的测试中运行它:
testFrame
| frame rolls |
rolls := Array with: 5 with: 4.
frame := Frame new: rolls.
I get the following error:
我收到以下错误:
alt text http://files.getdropbox.com/u/120566/junk/error.png
alt text http://files.getdropbox.com/u/120566/junk/error.png
How should I modify the #new message to be able to initialize Frame object with an array?
我应该如何修改#new消息以便能够用数组初始化Frame对象?
2 个解决方案
#1
I guess you failed adding the method new: correctly to Frame class. Are you sure you put it on the class side (Frame class) and not on the instance side (Frame)? To do it, click on the 'class' button, before adding your method new:.
我猜你没有将new方法添加到Frame类中。你确定你把它放在班级(Frame类)而不是实例侧(Frame)吗?要执行此操作,请在添加新方法之前单击“类”按钮:
#2
You really don't want to override new:
here. new:
is traditionally reserved for "Create an item of this integer size", and it doesn't surprise me that it's blowing up on you.
你真的不想覆盖新的:这里。 new:传统上保留用于“创建这个整数大小的项目”,并且它让我感到惊讶。
A more traditional name for the kind of constructor you want is fromArray:
, or perhaps even fromCollection:
which would probably have worked as you wished.
你想要的那种构造函数的一个更传统的名字是fromArray:,或者甚至是fromCollection:它可能已经按你的意愿工作了。
#1
I guess you failed adding the method new: correctly to Frame class. Are you sure you put it on the class side (Frame class) and not on the instance side (Frame)? To do it, click on the 'class' button, before adding your method new:.
我猜你没有将new方法添加到Frame类中。你确定你把它放在班级(Frame类)而不是实例侧(Frame)吗?要执行此操作,请在添加新方法之前单击“类”按钮:
#2
You really don't want to override new:
here. new:
is traditionally reserved for "Create an item of this integer size", and it doesn't surprise me that it's blowing up on you.
你真的不想覆盖新的:这里。 new:传统上保留用于“创建这个整数大小的项目”,并且它让我感到惊讶。
A more traditional name for the kind of constructor you want is fromArray:
, or perhaps even fromCollection:
which would probably have worked as you wished.
你想要的那种构造函数的一个更传统的名字是fromArray:,或者甚至是fromCollection:它可能已经按你的意愿工作了。