簡體   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