简体   繁体   中英

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:= 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:

  • rows:columns is a method which gives the matrix rows and columns
  • row:column is method that puts value into the matrix.

So, you created a class Matrix. It is similar to Array but specialized for matrix-like messages (the ones you used). Now you created two instances x and y of Matrix and put their entries using the messages you defined. 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?" The answer is: not in the class 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. Move the code that creates x and y to these methods and have these instances of Matrix held in temporaries of the method. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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