MATLAB Object-Oriented Programming

(Joyce) #1

Enumerations That Encapsulate Data


In this section...
“Enumeration Classes with Properties” on page 14-41
“Store Data in Properties” on page 14-41

Enumeration Classes with Properties


Enumeration classes can define properties to store data values. The enumeration
members represent specific values for these properties, which MATLAB assigns in the
class constructor. For information on defining enumeration classes, see “Define
Enumeration Classes” on page 14-5.

Store Data in Properties


NoteEnumeration classes that subclass built-in numeric or logical classes cannot define
or inherit properties. For more information on this kind of enumeration class, see
“Enumerations Derived from Built-In Types” on page 14-28.

Define properties in an enumeration class if you want to associate specific data with
enumeration members, but do not need to inherit arithmetic, ordering, or other
operations that MATLAB defines for specific built-in classes.

Representing Colors

Define an enumeration class to represent the RGB values of the colors in a color set. The
Colors class defines names for the colors, each of which uses the RGB values as
arguments to the class constructor:

classdef Colors
properties
R = 0
G = 0
B = 0
end
methods
function c = Colors(r, g, b)
c.R = r; c.G = g; c.B = b;

Enumerations That Encapsulate Data
Free download pdf