Matlab无法调用超类方法 [英] Matlab unable to call superclass method

查看:90
本文介绍了Matlab无法调用超类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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:

"@"在方法内,同名超类方法由调用说method @ superclass.@"的左操作数必须是方法名称.

"@" Within a method, a superclass method of the same name is called by saying method@superclass. The left operand of "@" must be the method name.

test 2方法显然存在于超类 test1 中,因此我不确定到底出了什么问题.

The test2 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无法调用超类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆