[英]How to save x and y in class Matrix i have created?
I have defined two objects X and Y both have same size array as an matrix 我定义了两个对象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"
Notes: 笔记:
So, you created a class Matrix. 因此,您创建了一个Matrix类。 It is similar to Array but specialized for matrix-like messages (the ones you used). 它与Array类似,但专门用于类似矩阵的消息(您使用的消息)。 Now you created two instances x and y of Matrix and put their entries using the messages you defined. 现在,您创建了Matrix的x和y的两个实例,并使用您定义的消息放置了它们的条目。 Everything is fine so far. 到目前为止一切都很好。
Now you want to "save" these instances, presumably to operate with them exercising other messages such as sum, multiplication, transposition, product by scalar, and so on. 现在,您要“保存”这些实例,大概是要与它们一起使用其他消息,例如求和,乘法,换位,标量乘积等。 Your question is "how do I save x and y?" 您的问题是“如何保存x和y?” The answer is: not in the class Matrix! 答案是: 不在Matrix类中! . 。
A good idea would be to create a subclass of TestCase, namely MatrixTest, and add there methods for testing such as testSum, testMultiplication, testScalarMultiplication, testTransposition, and so on. 一个好主意是创建TestCase的子类,即MatrixTest,并在其中添加用于测试的方法,例如testSum,testMultiplication,testScalarMultiplication,testTransposition等。 Move the code that creates x and y to these methods and have these instances of Matrix held in temporaries of the method. 将创建x和y的代码移到这些方法上,并将这些Matrix实例保存在该方法的临时实例中。 Something on the lines of: 在以下方面:
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>"
Generally speaking, you will not save instances of Matrix in Matrix, but in other classes that use matrices. 一般来说,您不会将Matrix的实例保存在Matrix中,而是保存在其他使用矩阵的类中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.