MATLAB Object-Oriented Programming

(Joyce) #1

Because this example uses a value class for the contained object, each instance of the
ClassExp has its own copy of the object. For example, suppose that you change the value
of the TimeProp property on the object contained by ClassExp objectb:


b.ObjProp.TimeProp = datestr(now)


ans =


08-Oct-2003 17:22:49


The copy of the object contained by object a is unchanged:


a.ObjProp.TimeProp


ans =


08-Oct-2003 17:16:08


Expressions in Handle Classes


Now consider the behavior if the contained object is a handle object:


classdef ContClass < handle
properties
TimeProp = datestr(now)
end
end


Creating two instances of the ClassExp class shows that MATLAB created an object
when it initialized the ContClass. MATLAB used a copy of the object’s handle for each
instance of the ClassExp class. Therefore, there is one ContClass object and the
ObjProp property of each ClassExp object contains a copy of its handle.


Create an instance of the ClassExp class and note the time of creation:


a = ClassExp;
a.ObjProp.TimeProp


ans =


08-Oct-2003 17:46:01


Create a second instance of the ClassExp class. The ObjProp contains the handle of the
same object:


Evaluation of Expressions in Class Definitions
Free download pdf