MATLAB Object-Oriented Programming

(Joyce) #1
For example, consider the class ColorClass and two subclasses, RGBColor and
HSVColor:

classdef ColorClass
properties
Color
end
end

The class RGBColor inherits the Color property from ColorClass. RGBColor stores a
color value defined as a three-element vector of red, green, and blue (RGB) values. The
constructor does not restrict the value of the input argument. It assigns this value directly
to the Color property.

classdef RGBColor < ColorClass
methods
function obj = RGBColor(rgb)
if nargin > 0
obj.Color = rgb;
end
end
end
end

The class HSVColor also inherits the Color property from ColorClass. HSVColor
stores a color value defined as a three-element vector of hue, saturation, brightness value
(HSV) values.

classdef HSVColor < ColorClass
methods
function obj = HSVColor(hsv)
if nargin > 0
obj.Color = hsv;
end
end
end
end

Create an instance of each class and concatenate them into an array. The RGBColor
object is dominant because it is the leftmost object and neither class defines a dominance
relationship:

crgb = RGBColor([1 0 0]);
chsv = HSVColor([0 1 1]);

10 Object Arrays

Free download pdf