ary = [crgb,chsv];
class(ary)
ans =
RGBColor
You can combine these objects into an array because MATLAB can pass the inferior object
of class HSVColor to the constructor of the dominant class. However, notice that the
Color property of the second RGBColor object in the array actually contains an
HSVColor object, not an RGB color specification:
ary(2).Color
ans =
HSVColor with properties:
Color: [0 1 1]
Avoid this undesirable behavior by:
- Implementing converter methods
- Performing argument checking in class constructors before assigning values to
properties
Converter Methods
If your class design requires object conversion, implement converter methods for this
purpose.
The ColorClass class defines converter methods for RGBColor and HSVColor objects:
classdef ColorClass
properties
Color
end
methods
function rgbObj = RGBColor(obj)
if isa(obj,'HSVColor')
rgbObj = RGBColor(hsv2rgb(obj.Color));
end
end
Concatenating Objects of Different Classes