Re: Overload method in a class
- From: "Steven Lord" <slord@xxxxxxxxxxxxx>
- Date: Thu, 11 Sep 2008 09:46:13 -0400
"Matthaeus Pilch" <Matthaeus.Pilch@xxxxxxxxxxxxxx> wrote in message
news:gab4it$f54$1@xxxxxxxxxxxxxxxxxxxxx
Hello together,
I'm programming object oriented in Matlab. I would like to overload a
method in a class:
function openFile(obj, permission)
end
function openFile(obj, filename, permission)
end
But I get the error message 'openFile appears to be incompatibly used or
redefined.' Is it possible to overlap methods in Matlab R2008a? When yes,
how?
MATLAB doesn't distinguish between methods based on the signature like this.
The error message is correct -- you're attempting to redefine openFile. If
you want to do this, you need to write one openFile method and use functions
like NARGIN and VARARGIN to determine how it was called:
classdef simpleclass73
properties
permission;
filename;
end
methods
function obj = simpleclass73(varargin)
if nargin < 1 || nargin > 2
error('simpleclass73:constructor:invalidNumberOfInputs', ...
'The simpleclass73 constructor requires 1 or 2
inputs.');
end
obj.permission = varargin{1};
if nargin > 1
obj.filename = varargin{2};
else
obj.filename = '';
end
end
function obj = openFile(obj, varargin)
switch nargin
case 2
% handle the openFile(obj, permission) case
permission = varargin{1};
filename = '';
case 3
% handle the openFile(obj, filename, permission) case
filename = varargin{1};
permission = varargin{2};
otherwise
% I don't know what you're trying to do
error('simpleclass73:openFile:unknownCase', ...
'You should call openFile with a permission or a
filename and a permission.');
end
fprintf('***** Diagnostics *****\nPermission: %s\nFilename:
%s\n', ...
permission, filename);
obj.permission = permission;
obj.filename = filename;
end
end
end
--
Steve Lord
slord@xxxxxxxxxxxxx
.
- References:
- Overload method in a class
- From: Matthaeus Pilch
- Overload method in a class
- Prev by Date: Re: Parameters Fitting
- Next by Date: Re: preallocate of cell array
- Previous by thread: Overload method in a class
- Next by thread: Overload method in a class
- Index(es):
Relevant Pages
|