MATLAB Object-Oriented Programming

(Joyce) #1

Properties:
Color


ary(2).Color


ans =


0 1 1


Defining a converter method in the superclass and adding better argument checking in
the subclass constructors produces more predicable results. Here is the RGBColor class
constructor with argument checking:


classdef RGBColor < ColorClass
methods
function obj = RGBColor(rgb)
if nargin == 0
rgb = [0 0 0];
else
if ~(isa(rgb,'double')...
&& size(rgb,2) == 3 ...
&& max(rgb) <= 1 && min(rgb) >= 0)
error('Specify color as RGB values')
end
end
obj.Color = rgb;
end
end
end


Your applications can require additional error checking and other coding techniques. The
classes in these examples are designed only to demonstrate concepts.


See Also


More About



  • “Implicit Class Conversion” on page 10-16

  • “Object Converters” on page 17-12

  • “Hierarchies of Classes — Concepts” on page 12-2


See Also
Free download pdf