Creating multiple instances of a class from within a class

8 次查看(过去 30 天)
Hey all,
In a trial from type trialclass, I would like to create multiple patients of type patientClass as such:
trial = trialClass();
trial = trial.addPatient(patientID);
To achieve this, trialClass and patientClass are respectively defined as followed:
classdef trialClass
properties
patient = patientClass.empty;
end
methods
function obj = trialClass()
end
function obj = addPatient(obj, patientID)
obj.patient = patientClass(patientID);
end
end
end
classdef patientClass
properties
patientID;
end
methods
function obj = patientClass(patientID)
obj.patientID = patientID;
end
end
end
However, when I call addPatient multiple times, the last created patient is overwritten. In other words, I end up with only one patient which has the last patientID.
Am I using the right data structure to store multiple patients in a trial?
Any suggestions are much appreciated!
  1 个评论
Daniel Shub
Daniel Shub 2011-10-6
If you are going to use an array of patientClass objects, you really want patientClass to handle being called with no arguments. The documentation is pretty clear about how object arrays are initialized, and requiring an argument is likely to cause problems do the road.

请先登录,再进行评论。

回答(1 个)

David Young
David Young 2011-10-6
The line
obj.patient = patientClass(patientID);
does indeed overwrite the single patient stored in trialClass.
One solution is to store an array of patients in trialClass. To implement the simplest version of this, just change the line above to
obj.patient(length(obj.patient)+1) = patientClass(patientID);
You'll then find that each call of addpatient will increase the length of the array, and you can recover all the patients' information from the property.
This simple approach can get you quite a long way, but it may not be adequate if you have large numbers of patients or you want to do complex analyses. You may find that a useful step in due course is to look at the containers.Map class, which would allow you, for example, to index the patients by their IDs.
  2 个评论
Bert
Bert 2011-10-6
Thank you for your reply.
Your first solution is known as an array of objects, right? I was under the impression that this construction violates the OOP-spirit. However, as I get more and more experience in MATLAB, I see this construction opted so often that I'm beginning to think I might be wrong about that assumption.
As for your second solution, that sounds like a more natural way to addressing the problem, since the patientID is the way information is linked to an individual patient. I will look into it, thanks for the pointer!
In conclusion, I would like to know if my assumption about arrays of objects not being OOP is faulty.
Daniel Shub
Daniel Shub 2011-10-6
I believe you assumption about arrays of objects not being oop is faulty. In fact, arrays of objects is one of the major advantages of MATLAB's oop.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by