Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 重载函数_Oop_Matlab_Overloading - Fatal编程技术网

Oop 重载函数

Oop 重载函数,oop,matlab,overloading,Oop,Matlab,Overloading,在Matlab中,有没有一种方法可以在同一个类中使用两个同名但参数不同的函数 简而言之:不,这是不可能的。 但是,您可以模仿这种行为: 显然,由于Matlab是一种动态语言,您可以传递任何类型的参数并进行检查 function foo(x) if isnumeric(x) disp(' Numeric behavior'); elseif ischar(x) disp(' String behavior'); end end 您还可以使用

在Matlab中,有没有一种方法可以在同一个类中使用两个同名但参数不同的函数

简而言之:不,这是不可能的。

但是,您可以模仿这种行为:

显然,由于Matlab是一种动态语言,您可以传递任何类型的参数并进行检查

function foo(x)
    if isnumeric(x)
        disp(' Numeric behavior');
    elseif ischar(x)
        disp(' String behavior');
    end
end
您还可以使用varargin,检查参数的数量,并更改行为

function goo(varargin)
    if nargin == 2
        disp('2 arguments behavior');
    elseif nargin == 3
        disp('3 arguments behavior');   
    end
end

安德烈已经给出了正确的答案。然而,我已经做了一些实验一段时间了,我想展示一下我认为的另一种相对简单的方法,它有一些好处。此外,这也是MATLAB用于其内置函数的一种方法

我指的是这种传递参数的键值对方式:

x = 0:pi/50:2*pi;
y = sin(x);
plot(x, y, 'Color', 'blue', 'MarkerFaceColor', 'green');
解析一个
varargin
单元格数组的方法有很多种,但是我发现到目前为止最干净的方法是使用MATLAB
inputParser

例如:

function makeSandwiches(amount, varargin)
%// MAKESANDWICHES Make a number of sandwiches.
%// By default, you get a ham and egg sandwich with butter on white bread.
%// Options:
%// amount       : number of sandwiches to make (integer)
%// 'butter'     : boolean
%// 'breadType'  : string specifying 'white', 'sourdough', or 'rye'
%// 'topping'    : string describing everything you like, we have it all!

p = inputParser();                          %// instantiate inputParser
p.addRequired('amount', @isnumeric);        %// use built-in MATLAB for validation
p.addOptional('butter', 1, @islogical);
p.addOptional('breadType', 'white', ...     %// or use your own (anonymous) functions
    @(x) strcmp(x, 'white') || strcmp(x, 'sourdough') || strcmp(x, 'rye'));
p.addOptional('toppings', 'ham and egg', @(x) ischar(x) || iscell(x))
p.parse(amount, varargin{:});               %// Upon parsing, the variables are
                                            %// available as p.Results.<var>

%// Get some strings
if p.Results.amount == 1
    stringAmount = 'one tasty sandwich';
else
    stringAmount = sprintf('%d tasty sandwiches', p.Results.amount);
end

if p.Results.butter
    stringButter = 'with butter';
else
    stringButter = 'without butter';
end

%// Make the sandwiches
fprintf(['I made you %s %s from %s bread with %s and taught you ' ...
    'something about input parsing and validation in MATLAB at ' ...
    'the same time!\n'], ...
    stringAmount, stringButter, p.Results.breadType, p.Results.toppings);

end
函数制作三明治(数量,varargin)
%//制作三明治制作许多三明治。
%//默认情况下,你会得到一个火腿蛋三明治,白面包上涂有黄油。
%//选项:
%//数量:要制作的三明治数量(整数)
%//“黄油”:布尔值
%//“面包类型”:指定“白色”、“酸面包”或“黑麦”的字符串
%//“topping”:描述您喜欢的所有内容的字符串,我们都有!
p=inputParser();%//实例化inputParser
p、 addRequired('amount',@isnumeric);%//使用内置的MATLAB进行验证
p、 addOptional('butter',1,@islogical);
p、 addOptional('breadType','white',…%//或使用您自己的(匿名)函数
@(x) strcmp(x,'白色')| | strcmp(x,'酸面包')| | strcmp(x,'黑麦');
p、 添加可选(“配料”、“火腿和鸡蛋”)@(x)ischar(x)| | iscell(x))
p、 解析(金额,varargin{:});%//在解析时,变量是
%//可作为p.Results获得。
%//找些线索
如果p.Results.amount==1
stringAmount=‘一份美味三明治’;
其他的
stringAmount=sprintf(“%d个美味三明治”,p.Results.amount);
结束
如果是黄油
stringButter=‘加黄油’;
其他的
stringButter=‘不含黄油’;
结束
%//做三明治
fprintf([“我用%s面包和%s为您制作了%s%s,并教您”。。。
'有关MATLAB中输入解析和验证的信息,请访问'。。。
'同一时间!\n']。。。
stringAmount、stringButter、p.Results.breadType、p.Results.toppings);
结束
(注释后斜杠,因为SO不支持MATLAB语法突出显示)

此方法的附加好处是:
-可以设置默认值(如Python,您可以在函数签名中设置
arg=val

-以简单方式执行输入验证的可能性
-您只需记住选项名称,而不必记住它们的顺序,因为顺序并不重要

缺点:
-在执行许多函数调用时,可能会有一些开销变得非常大,而在其他情况下(未测试)
-您必须使用
inputParser
类中的
Results
属性,而不是直接使用变量

相关/可能的重复项:,