Ways to Use Properties
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.
Calculate its value based on the current value of other data. See Property Get Methods.
Execute a function to determine if an attempt to assign a value meets a certain criteria. See Property Set Methods.
Trigger an event notification when any attempt is made to get or set its value. See Property-Set and Query Events.
Control access by code to the property values. See the
GetAccessattributes Property Attributes.
Control whether its value is saved with the object in a MAT-file. See Save and Load Objects.
For an example of a class that defines and uses a class, see Creating a Simple Class.
Types of Properties
There are two types of properties:
Stored properties — Use memory and are part of the object
Dependent properties — No allocated memory and the get access method calculates the value when queried
Features of Stored Properties
Property value is stored when you save the object to a MAT-file
Can assign a default value in the class definition
Can restrict property value to a specific class and size
Can execute validation functions to control allowed property value (default and assigned)
Can use a set access method to control possible values when set
When to Use Stored Properties
You want to be able to save the property value in a MAT-file
The property value is not dependent on other property values
Features of Dependent Properties
Dependent properties save memory because property values that depend on other values are calculated only when needed.
When to Use Dependent Properties
Define properties as dependent when you want to:
Compute the value of a property from other values (for example, you can compute area from
Provide a value in different formats depending on other values. For example, the size of a push button in values determined by the current setting of its
Provide a standard interface where a particular property is or is not used, depending on other values. For example, different computer platforms can have different components on a toolbar).