2013-01-10 1 views
3

코드에 직접 들어가 봅시다. 두 가지 수업이 있습니다. 수퍼 클래스는 하위 클래스는MATLAB : parfor에서 수퍼 클래스 메서드 호출

classdef Child < Parent 
    methods 
    function this = Child() 
     this = [email protected](); 
    end 

    function say(this, message) 
     for i = 1 
     % This one works... 
     [email protected](this, message); 
     end 

     parfor i = 1 
     % ... but this one does not. 
     [email protected](this, message); 
     end 
    end 
    end 
end 

질문입니다

classdef Parent 
    methods 
    function this = Parent() 
    end 

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

입니다 : 어떻게 추가 방법을 도입하지 않고 루프를 작동하게하는? 현재로서는 "기본 클래스 메서드는 같은 이름의 하위 클래스 메서드에서만 명시 적으로 호출 할 수 있습니다."라는 오류가 발생합니다. 고맙습니다.

감사합니다, 이반

답변

1

난 당신이 전화를 한 후 명시 적으로 parfor 루프를 호출하기 전에 Parentthis 캐스팅해야하고, 할 수있다 생각 Parent 방법은 명시 적으로 say :

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

그렇게하기 위해, 입력 인수를 허용하도록 Parent의 생성자를 수정해야합니다.

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

Parent 경우 Child은 실제 수업은 아마처럼, 당신은 새로 건설 된 Parent 객체에 Child의 속성을 할당 할 것 if 문 다음 몇 가지 코드를 포함 할 것이다, 속성을 가지고 있었다.

관련 문제