2012-02-27 48 views
1

我想创建一个非常简单的Vector类作为Smalltalk中Array的子​​类。我创建类的代码如下所示:Basic Smalltalk子类

Array subclass: #Vector 
Vector comment: 'I represent a Vector of integers' 

Vector class extend [ 
new [ 
    | r | 
    <category: 'instance creation'> 
    r := super new. 
    r init. 
    ^r 
    ] 
] 

Vector extend [ 
init [ 
    <category: 'initialization'> 
    ] 
] 

显然我还没有写任何方法,但我只是试图让这部分工作第一。创建后的类如上,如果我输入: 五:=矢量新:4 我得到错误:

Object: Vector error: should not be implemented in this class, use #new instead 
SystemExceptions.WrongMessageSent(Exception)>>signal (ExcHandling.st:254) 
SystemExceptions.WrongMessageSent class>>signalOn:useInstead: (SysExcept.st:1219) 
Vector class(Behavior)>>new: (Builtins.st:70) 
UndefinedObject>>executeStatements (a String:1) 
nil 

我曾以为,既然是数组的一个子类,我可以创造一个矢量这种方式。做这件事的最好方法是什么?谢谢!

编辑 - 我想通了。在深入阅读教程之后,我发现我需要包含<形状:#指针>

+0

我觉得你有这个人的同样的问题:http://stackoverflow.com/questions/5613363/gnu-smalltalk-problem-with-example-in-tutorial-object-creation – andrewdotnich 2012-02-27 23:58:00

+0

我看着那个帖子,改变了这些事情,但我仍然得到了同样的结果。 – FlapsFail 2012-02-28 02:36:56

回答

4

数组是一种特殊的类,具有不同长度的可索引实例。

在GNU Smalltalk中(你似乎使用)Array类被声明为:

​​

要继承这个行为,你可以使用:

Array subclass: Vector [<shape: #inherit>] 

但在大多数情况下,它使制作一个封装了Array的类而不是从Array继承的类更有意义。

这也是值得指出的是OrderedCollection是Smalltalk的相当于从C 矢量容器++和Java的。