MATLAB Object-Oriented Programming

(Joyce) #1

  • The MySuper class defines a loadobj method to enable an object of this class to be
    loaded directly.

  • The subclass loadobj method calls a method named reload after it constructs the
    subclass object.

  • reload first calls the superclass reload method to assign superclass property values
    and then assigns the subclass property value.


classdef MySuper
properties
X
Y
end
methods
function S = saveobj(obj)
S.PointX = obj.X;
S.PointY = obj.Y;
end
function obj = reload(obj,S)
obj.X = S.PointX;
obj.Y = S.PointY;
end
end
methods (Static)
function obj = loadobj(S)
if isstruct(s)
obj = MySuper;
obj = reload(obj,S);
end
end
end
end

Call the superclass saveobj and loadobj methods from the subclass saveobj and
loadobj methods.

classdef MySub < MySuper
properties
Z
end
methods
function S = saveobj(obj)
S = saveobj@MySuper(obj);
S.PointZ = obj.Z;

13 Saving and Loading Objects

Free download pdf