MATLAB:从parfor调用超类'方法

时间:2013-01-10 09:01:59

标签: matlab inheritance

让我们直接看到代码。有两个班。超类是

classdef Parent
  methods
    function this = Parent()
    end

    function say(this, message)
      fprintf('%s\n', message);
    end
  end
end

子类是

classdef Child < Parent
  methods
    function this = Child()
      this = this@Parent();
    end

    function say(this, message)
      for i = 1
        % This one works...
        say@Parent(this, message);
      end

      parfor i = 1
        % ... but this one does not.
        say@Parent(this, message);
      end
    end
  end
end

问题是:如何在不引入任何其他方法的情况下使第二个循环工作?至于现在,它引发了一个错误,说&#34;基类方法只能从同名的子类方法中显式调用。&#34;谢谢。

此致 伊万

1 个答案:

答案 0 :(得分:1)

我认为您可能需要在调用this循环之前明确地将Parent强制转换为parfor,然后明确调用Parent方法say:< / p>

this2 = Parent(this);
parfor i = 1:1
  say(this2, message);
end

为此,您需要修改Parent的构造函数以接受输入参数:

function this = Parent(varargin)
    if nargin == 1
        this = Parent();
    end
end

如果ParentChild具有属性,正如您的真实类可能做的那样,您将在if语句后面添加一些代码,以便将Child属性分配给新构造的Parent对象。