MATLAB Object-Oriented Programming

(Joyce) #1
Implementation

The SharedData class is a handle class, which enables you to reference the same object
data from multiple handle variables:

classdef SharedData < handle
properties
Data1
Data2
end
end

The UseData class is the stub of a class that uses the data stored in the SharedData
class. The UseData class stores the handle to a SharedData object in a constant
property.

classdef UseData
properties (Constant)
Data = SharedData
end
% Class code here
end

The Data property contains the handle of the SharedData object. MATLAB constructs
the SharedData object when loading the UseData class. All subsequently created
instances of the UseData class refer to the same SharedData object.

To initialize the SharedData object properties, load theUseData class by referencing the
constant property.

h = UseData.Data

h =

SharedData with properties:

Data1: []
Data2: []

Use the handle to the SharedData object to assign data to property values:

h.Data1 = 'MyData1';
h.Data2 = 'MyData2';

Each instance of the UseData class refers to the same handle object:

4 Static Data

Free download pdf