繁体   English   中英

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

[英]How to save x and y in class Matrix i have created?

我定义了两个对象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是一种提供矩阵行和列的方法
  • row:column是将值放入矩阵的方法。

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

现在,您要“保存”这些实例,大概是要与它们一起使用其他消息,例如求和,乘法,换位,标量乘积等。 您的问题是“如何保存x和y?” 答案是: 不在Matrix类中!

一个好主意是创建TestCase的子类,即MatrixTest,并在其中添加用于测试的方法,例如testSum,testMultiplication,testScalarMultiplication,testTransposition等。 将创建x和y的代码移到这些方法上,并将这些Matrix实例保存在该方法的临时实例中。 在以下方面:

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>"

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

暂无
暂无

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

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