MATLAB Object-Oriented Programming

(Joyce) #1
ans =

1 0 0

You cannot change a property value:

red.G = 1;

You cannot set the read-only property 'G' of Colors.

Handle-Based Enumeration Classes


Handle-based enumeration classes that define properties are mutable. Derive
enumeration classes from the handle class when you must be able to change property
values on instances of that class.

NoteYou cannot derive an enumeration class from matlab.mixin.Copyable because
the number of instances you can create are limited to the ones defined inside the
enumeration block.

An Enumeration Member Remains Constant

Given a handle-based enumeration class with properties, changing the property value of
an instance causes all references to that instance to reflect the changed value.

For example, the HandleColors enumeration class associates RGB values with color
names, the same as the Colors class in the previous example. However, HandleColors
derives from handle:

classdef HandleColors < handle
properties
R = 0
G = 0
B = 0
end
methods
function c = HandleColors(r, g, b)
c.R = r; c.G = g; c.B = b;
end
end
enumeration

14 Enumerations

Free download pdf