MATLAB Object-Oriented Programming

(Joyce) #1

switch c
case 'E'
v = obj.DollarAmount / 1.1;
case 'P'
v = obj.DollarAmount / 1.5;
otherwise
v = obj.DollarAmount;
end
format bank
value = v;
end
end
end


Calculate Dependent Property Value


One application of a property get method is to determine the value of a property only
when you need it, and avoid storing the value. To use this approach, set the property
Dependent attribute to true:


properties (Dependent = true)
Prop
end


The get method for the Prop property determines the value of that property and assigns
it to the object from within the method:


function value = get.Prop(obj)
value = calculateValue;
...
end


This get method calls a function or static method called calculateValue to calculate
the property value and returns value as a result. The property get method can take
whatever action is necessary within the method to produce the output value.


For an example of a property get method, see “Calculate Data on Demand” on page 3-23.


When to Use Set Methods with Dependent Properties


Although a dependent property does not store its value, you can define a set method for a
dependent property to enable code to set the property.


Set and Get Methods for Dependent Properties
Free download pdf