在MATLAB中如何强制派生类调用基类函数?

5

基类拥有函数f。派生类重写了函数f。我想为派生类的对象调用基类的函数f。我该怎么做?

以下是代码示例。

    classdef base

        methods ( Access = public )
            function this = f( this )
                disp( 'at base::f' );
            end

        end
    end

    classdef derived < base

        methods ( Access = public )
            function this = f( this )
                % HERE I WANT TO CALL base::f
                this@base.f(); % this is an error

                disp( 'at derived::f' );
            end

        end
    end

d = derived();
d.f();
% here the result should be
% at base::f
% at derived::f
1个回答

8

替代

this@base.f();

it's

f@base(this)

@Vahagn:它将显示derived::f,因为该语句在对f@base的调用之后执行。不过我不理解无限循环。 - Jonas
2
@Vahagn:这是文档中的链接:http://www.mathworks.com/help/techdoc/matlab_oop/bsa1q42.html - Jonas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接