MATLAB Object-Oriented Programming

(Joyce) #1

z = repmat(obj,varargin{:});
end
end
end
end


Full Class Listing


Here is the Color class definition with the overloaded methods.


NoteIn actual practice, the Color class requires error checking, color space
conversions, and so on. This overly simplified version illustrates the implementation of the
overloaded methods.


classdef Color
properties
ColorValues = [0,0,0]
ColorSpace = 'RGB'
end
methods
function obj = Color(cSpace,values)
if nargin > 0
obj.ColorSpace = cSpace;
obj.ColorValues = values;
end
end
end
methods (Static)
function z = zeros(varargin)
if (nargin == 0)
% For zeros('ClassName')
z = Color;
elseif any([varargin{:}] <= 0)
% For zeros with any dimension <= 0
z = Color.empty(varargin{:});
else
% For zeros(m,n,...,'ClassName')
% Use property default values
z = repmat(Color,varargin{:});
end
end
function r = randi(varargin)
if (nargin == 0)
% For randi('ClassName')
r = Color('RGB',randi(255,[1,3]));
elseif any([varargin{2:end}] <= 0)
% For randi with any dimension <= 0
r = Color.empty(varargin{2:end});
else
% For randi(max,m,n,...,'ClassName')
if numel([varargin{:}]) < 2


Class Support for Array-Creation Functions
Free download pdf