Matrix 如何在我创建的类矩阵中保存x和y?

Matrix 如何在我创建的类矩阵中保存x和y?,matrix,smalltalk,visualworks,Matrix,Smalltalk,Visualworks,我已经定义了两个对象X和Y,它们都具有与矩阵相同大小的数组 x:= Matrix new. x rows: 2 columns: 2; row: 1 column: 1 put: 2; row: 2 column: 1 put: 2; row: 1 column: 2 put: 2; row: 2 column: 2 put: 2. #(2 2 2 2) "The x returns an array"

我已经定义了两个对象X和Y,它们都具有与矩阵相同大小的数组

    x:= Matrix new.
    x
      rows: 2 columns: 2;
      row: 1 column: 1 put: 2;
      row: 2 column: 1 put: 2;
      row: 1 column: 2 put: 2;
      row: 2 column: 2 put: 2.
    #(2 2 2 2)  "The x returns an array"
    y := Matrix new
    y
     rows: 2 columns: 2;
     row: 1 column: 1 put: 2;
     row: 2 column: 1 put: 2;
      row: 1 column: 2 put: 2;
     row: 2 column: 2 put: 2.
    #(2 2 2 2) "The object y returns an array"
注意事项:

  • rows:columns是一种提供矩阵行和列的方法
  • 行:列是将值放入矩阵的方法

  • 因此,您创建了一个类矩阵。它类似于数组,但专门用于类似矩阵的消息(您使用的消息)。现在,您创建了矩阵的两个实例x和y,并使用您定义的消息放置它们的条目。到目前为止一切都很好

    现在您想要“保存”这些实例,大概是为了使用它们来执行其他消息,例如求和、乘法、换位、按标量乘积等等。你的问题是“如何保存x和y?”答案是:不在类矩阵中

    一个好主意是创建TestCase的一个子类,即MatrixTest,并添加testSum、Test乘法、testScalarMultiplication、testTransposition等测试方法。将创建x和y的代码移动到这些方法中,并将这些矩阵实例保存在该方法的临时表中。关于以下内容:

    MatrixText >> testSum
    | x y z |
    x := Matrix new rows: 2 columns: 2.
    x row: 1 column: 1 put: 2.
    x row: 1 column: 2 put: 2.
    "<etc>"
    y := Matrix new rows: 2 columns: 2.
    y row: 1 column: 1 put: 2.
    "<etc>"
    z = x + y (you need to define the method + in Matrix!).
    self assert: (z row: 1 column: 1) = 4.
    "<etc>"
    
    MatrixText>>testSum
    |x y z|
    x:=矩阵新行:2列:2。
    x行:1列:1放入:2。
    x行:1列:2放入:2。
    ""
    y:=矩阵新行:2列:2。
    y行:1列:1放入:2。
    ""
    z=x+y(您需要在矩阵中定义方法+)。
    自断言:(z行:1列:1)=4。
    ""
    

    一般来说,您不会在Matrix中保存Matrix的实例,而是在其他使用Matrix的类中保存。

    问题是什么?好像
    行:列:
    行:列:put:
    是一种用户定义的方法,因此很难调试代码并准确地告诉您错误所在。你还得到了一个数组吗?或者您正在尝试重新获取阵列。发现评论有点模棱两可。