MATLAB Object-Oriented Programming

(Joyce) #1
Class Name Method Called If Prototype Method Does Not Exist

If your class implements a class name syntax, but does not implement a prototype object
syntax for a particular function, you can still call both syntaxes. For example, if you
implement a static zeros method only, you can call:

zeros(...,'like',MyClass(...))

In the case in which you call the prototype object syntax, MATLAB first searches for a
method named zerosLike. If MATLAB cannot find this method, it calls for the zeros
static method.

This feature is useful if you only need the class name to create the array. You do not need
to implement both methods to support the complete array-creation function syntax. When
you implement only the class name syntax, a call to a prototype object syntax is the same
as the call to the class name syntax.

Implement Support for Array-Creation Functions


Use two separate methods to support an array-creation function. One method implements
the class name syntax and the other implements the prototype object syntax.

For example, to support the zeros function:


  • Implement the class name syntax:


zeros(...,'ClassName')

As a Static method:

methods (Static)
function z = zeros(varargin)
...
end
end


  • Implement the prototype object syntax:


zeros(...,'like',obj)

As a Hidden method with the char vector 'Like' appended to the name.

methods (Hidden)
function z = zerosLike(obj,varargin)

9 Methods — Defining Class Operations

Free download pdf