问题描述
我是Matlab的新手,在调用超类方法时遇到了一些问题.
I'm new to Matlab and am facing some problems with calling superclass methods.
我有以下代码:
超类test1:
classdef test1 < handle
methods
function obj = test1()
end
function test2(obj)
disp(1);
end
end
end
子类测试:
classdef test < test1 & handle
properties
foo = 1;
end
methods
function obj = test()
obj = obj@test1();
end
function a = bar(obj)
superclasses(obj)
test2@test1(obj)
end
end
end
继承工作正常;superclasses函数将 test1
显示为 test
的超类.但是,当我调用 test2 @ test1(obj)
时,它返回错误:
The inheritance works correctly; the superclasses function shows test1
as a superclass of test
. However, when I call test2@test1(obj)
, it returns an error:
test
2方法显然存在于超类 test1
中,因此我不确定到底出了什么问题.
The test
2 method obviously exists within the superclass test1
, so I'm not sure what exactly is going wrong.
推荐答案
仅当您的超类和子类中的方法名称相同且调用位于同名子类方法内时,才可以使用@语法.否则,您可以直接调用该方法,因为不会造成混淆.因此,请使用test2(obj)代替 test2 @ test1(obj)
.
You can use the @ syntax only if the method names in your superclass and child class are same and the call is within the child class method with the same name. Otherwise you can just call the method directly since there is no confusion. So instead of test2@test1(obj)
just use test2(obj).
您也不需要在子类中再次将handle指定为超类.
You also do not need to specify handle as a super class again in your child class.
这篇关于Matlab无法调用超类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!