MATLAB Object-Oriented Programming

(Joyce) #1

Set and Get Methods for Dependent Properties


In this section...
“Calculate Dependent Property Value” on page 8-63
“When to Use Set Methods with Dependent Properties” on page 8-63
“Private Set Access with Dependent Properties” on page 8-64

Dependent properties do not store data. The value of a dependent property depends on
some other value, such as the value of a nondependent property.

Dependent properties must define get-access methods (get.PropertyName) to
determine a value for the property when the property is queried.

The values returned by dependent property get methods are not considered when testing
for object equality using isequal and isequaln.

To be able to set the value of a dependent property, the property must define a set access
method (set.PropertyName). The property set access method usually assigns the value
to another, nondependent property for storage of the value.

For example, the Account class returns a value for the dependent Balance property that
depends on the value of the Currency property. The get.Balance method queries the
Currency property before calculating a value for the Balance property.

MATLAB calls the get.Balance method when the Balance property is queried. You
cannot call get.Balance explicitly.

Here is a partial listing of the class showing a dependent property and its get method:

classdef Account
properties
Currency
DollarAmount
end
properties (Dependent)
Balance
end
...
methods
function value = get.Balance(obj)
c = obj.Currency;

8 Properties — Storing Class Data

Free download pdf