MATLAB Object-Oriented Programming

(Joyce) #1

The Colors class encapsulates the definitions of a standard set of colors. You can change
the enumeration class definition of the colors and not affect functions that use the
enumerations.


Enumerations Defining Categories


The Cars class defines categories used to inventory automobiles. The Cars class derives
from the CarPainter class, which derives from handle. The abstract CarPainter class
defines a paint method, which modifies the Color property when a car is painted
another color.


The Cars class uses the Colors enumeration members to specify a finite set of available
colors. The exact definition of any given color can change independently of the Cars
class.


classdef Cars < CarPainter
enumeration
Hybrid (2,'Manual',55,Colors.Reddish)
Compact(4,'Manual',32,Colors.Greenish)
MiniVan(6,'Automatic',24,Colors.Blueish)
SUV (8,'Automatic',12,Colors.Yellowish)
end
properties (SetAccess = private)
Cylinders
Transmission
MPG
Color
end
methods
function obj = Cars(cyl,trans,mpg,colr)
obj.Cylinders = cyl;
obj.Transmission = trans;
obj.MPG = mpg;
obj.Color = colr;
end
function paint(obj,colorobj)
if isa(colorobj,'Colors')
obj.Color = colorobj;
else
[~,cls] = enumeration('Colors');
disp('Not an available color')
disp(cls)
end
end


Enumerations That Encapsulate Data
Free download pdf