MATLAB Object-Oriented Programming

(Joyce) #1

CurrentValue
end
properties (Transient)
Status
end
methods
function obj = LabResult(cv)
obj.CurrentValue = cv;
obj = assignStatus(obj);
end
function obj = assignStatus(obj)
v = obj.CurrentValue;
if v < 10
obj.Status = 'Too low';
elseif v >= 10 && v < 100
obj.Status = 'In range';
else
obj.Status = 'Too high';
end
end
end
methods (Static)
function obj = loadobj(s)
if isstruct(s)
cv = s.CurrentValue;
obj = LabResults(cv);
else
obj = assignStatus(s);
end
end
end
end


The LabResults class uses loadobj to determine the status of a given test value. This
approach provides a way to:



  • Modify the criteria for determining status

  • Ensure that objects always use the current criteria


You do not need to implement a saveobj method.


Initialize Objects When Loading
Free download pdf