MATLAB Object-Oriented Programming

(Joyce) #1
B
end
methods
function c = SyntaxColors(r, g, b)
c.R = r; c.G = g; c.B = b;
end
end
enumeration
Error (1, 0, 0)
Comment (0, 1, 0)
Keyword (0, 0, 1)
String (1, 0, 1)
end
end

When you refer to an enumeration member, the constructor initializes the property
values:

e = SyntaxColors.Error;
e.R

ans =

1

Because SyntaxColors is a value class (it does not derive from handle), only the class
constructor can set property values:

e.R = 0

You cannot set the read-only property 'R' of SyntaxColors.

For more information on enumeration classes that define properties, see “Mutable Handle
vs. Immutable Value Enumeration Members” on page 14-34.

Enumeration Class Constructor Calling Sequence


Each statement in an enumeration block is the name of an enumeration member,
optionally followed by an argument list. If the enumeration class defines a constructor,
MATLAB calls the constructor to create the enumerated instances.

MATLAB provides a default constructor for all enumeration classes that do not explicitly
define a constructor. The default constructor creates an instance of the enumeration
class:

14 Enumerations

Free download pdf