MATLAB Object-Oriented Programming

(Joyce) #1

  • zeros(varargin) for “class name” methods

  • zeros(obj,varargin) for “like prototype object” methods


Sample Class

The Color class represents a color in a specific color space, such as, RGB, HSV, and so on.
The discussions in “Class Name Method Implementations” on page 9-42 and “Prototype
Object Method Implementation” on page 9-44 use this class as a basis for the overloaded
method implementations.

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
end

Class Name Method Implementations

The zeros function strips the final ClassName char vector and uses it to form the call to
the static method in the Color class. The arguments passed to the static method are the
array dimension arguments.

Here is an implementation of a zeros method for the Color class. This implementation:


  • Defines the zeros method as Static (required)

  • Returns a scalar Color object if the call to zeros has no dimension arguments

  • Returns an empty array if the call to zeros has any dimensions arguments equal to 0.

  • Returns an array of default Color objects. Use repmat to create an array of the
    dimensions specified by the call to zeros.


classdef Color
...
methods (Static)

9 Methods — Defining Class Operations

Free download pdf