【问题标题】:Matlab unable to call superclass methodMatlab无法调用超类方法
【发布时间】:2023-03-11 12:22:01
【问题描述】:

我是 Matlab 新手,在调用超类方法时遇到了一些问题。

我有这个代码:

超类测试1:

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) 时,它返回一个错误:

"@" 在方法中,同名的超类方法被调用 说方法@超类。 “@”的左操作数必须是方法 名字。

test2 方法显然存在于超类test1 中,所以我不确定到底出了什么问题。

【问题讨论】:

    标签: matlab oop matlab-class


    【解决方案1】:

    只有当你的超类和子类中的方法名称相同并且调用在同名的子类方法中时,你才能使用@语法。否则,您可以直接调用该方法,因为没有混淆。所以不要使用test2@test1(obj),而是使用 test2(obj)。

    您也不需要在子类中再次将句柄指定为超类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 2011-10-24
      • 2015-04-23
      • 2012-12-30
      相关资源
      最近更新 更多