什么';Matlab类方法是抽象的还是静态的?
Matlab允许类方法既是什么';Matlab类方法是抽象的还是静态的?,matlab,oop,static,abstract,Matlab,Oop,Static,Abstract,Matlab允许类方法既是抽象的又是静态的,这与许多OO语言不同。然而,在尝试了几个案例之后,我不清楚如何在实际代码中使用它。考虑这个样本抽象类: classdef (Abstract) TestParent methods (Access = public, Abstract = true, Static = true) f() end methods (Access = public, Static = true) function
抽象的
又是静态的
,这与许多OO语言不同。然而,在尝试了几个案例之后,我不清楚如何在实际代码中使用它。考虑这个样本抽象类:
classdef (Abstract) TestParent
methods (Access = public, Abstract = true, Static = true)
f()
end
methods (Access = public, Static = true)
function g()
disp('Test g');
TestParent.f(); % This line will cause problems
end
end
end
并考虑这个具体的子类:
classdef TestChild < TestParent
methods (Access = public, Static = true)
function f()
disp('f child');
end
end
end
在使用父类的命令行上,我得到:
>> TestChild.f()
f child
>> TestChild.g()
Test g
Error using TestParent.f
Method 'f' is not defined for class 'TestParent' or is removed from MATLAB's search path.
Error in TestParent.g (line 13)
TestParent.f();
>> TestParent.g()
Test g
Error using TestParent.f
Method 'f' is not defined for class 'TestParent' or is removed from MATLAB's search path.
Error in TestParent.g (line 13)
TestParent.f();
因此,总而言之:
- 我可以毫无问题地调用子类的
。这是有意义的,因为它在子类中有一个具体的实现。到目前为止还不错f
- 如果我试图使用父类名称作为限定符调用
,那么当需要调用抽象静态方法g
时,代码就会崩溃。这是有意义的,因为父级没有f
的实现,并且调用路径没有经过任何具体的子级f
- 如果我尝试从子类调用
,它会识别子类没有具有该名称的本地方法,并转到父类,在父类中查找并尝试运行父类的g
实现。在本例中,我希望它能够成功运行,因为调用路径在逻辑上确实意味着应该运行的g
的具体实现。尽管如此,它还是崩溃了f
最后一个对我来说是真正的惊喜。我希望,如果静态和抽象有什么意义,那就意味着这是受支持的。鉴于这显然不受支持,我不明白为什么这种关键字组合有意义。有人能给出这个用例吗?静态方法也可以在实例上调用。因此,如果您有一个实例,那么重定向将正确地用于静态方法。似乎相关的异端方法也可以在实例上调用。因此,如果您有一个实例,那么重定向将正确地用于静态方法。在这里似乎很相关