MATLAB Object-Oriented Programming

(Joyce) #1

Ways to Use Properties


In this section...
“What Are Properties” on page 8-2
“Types of Properties” on page 8-3

What Are Properties


Properties encapsulate the data that belongs to instances of classes. Data contained in
properties can be public, protected, or private. This data can be a fixed set of constant
values, or it can depend on other values and calculated only when queried. You control
these aspects of property behaviors by setting property attributes and by defining
property-specific access methods.

Flexibility of Object Properties

In some ways, properties are like fields of a struct object. However, storing data in an
object property provides more flexibility. Properties can:


  • Define a constant value that you cannot change outside the class definition. See
    “Define Class Properties with Constant Values” on page 15-2.

  • Calculate its value based on the current value of other data. See “Property Get
    Methods” on page 8-60.

  • Execute a function to determine if an attempt to assign a value meets a certain
    criteria. See “Property Set Methods” on page 8-56.

  • Trigger an event notification when any attempt is made to get or set its value. See
    “Property-Set and Query Events” on page 11-17.

  • Control access by code to the property values. See the SetAccess and GetAccess
    attributes “Property Attributes” on page 8-9.

  • Control whether its value is saved with the object in a MAT-file. See “Save and Load
    Objects” on page 13-2.


For an example of a class that defines and uses a class, see “Create a Simple Class” on
page 2-2.

8 Properties — Storing Class Data

Free download pdf