2016-11-15 6 views
1

MATLAB 핸들 클래스의 속성 postSet은 매우 편리하지만 중첩 클래스를 개별적으로 트리거 할 수있어서 기쁩니다. 그림은 두 하위 클래스와 최소한 예 :중첩 된 Matlab 속성 수신기

classdef parentClass < handle 
    properties (SetObservable = true) 
     childClass 
    end 

    methods 
     function this = parentClass() 
      this.childClass = childClass(); 
     end 
    end 
end 

및 예 스크립트

classdef childClass < handle 
    properties (SetObservable = true) 
     value 
    end 

    methods 
     function this = childClass() 
      this.value = 0; 
     end 
    end 
end 

"runTest"

p = parentClass(); 

addlistener(p.childClass,'value','PostSet',@(o,e)disp('child value set')); 
addlistener(p,'childClass','PostSet',@(o,e)disp('parent value set')); 

p.childClass.value = 1; 

결과 (예상)

>> runTest 
child value set 

하우 버전, 나는 결과가 될 것 같은 것을 모두 수준에 대한 속성 변화를 감지하는 우아한 방법을 찾고 있어요 :이 작업을 수행하는

>> runTest 
child value set 
parent value set 

답변

0

한 가지 방법은 PostSet 이벤트가 기본적으로 트리거됩니다 사실을 사용하는 경우에도 이전 값과 현재 값이 같을 때 parentClass의 생성자에서, 우리는 childClassPostSet 이벤트에 대한 리스너를 추가하고 이벤트 핸들러 만 childClass 객체 재 할당 : 당신이 이벤트 있도록이 구현 더 조심해야 할 것

classdef parentClass < handle 
    properties (SetObservable = true) 
     childClass 
    end 

    methods 
     function this = parentClass() 
      this.childClass = childClass(); 
      addlistener(this.childClass,'value','PostSet', @this.handlePropEvent); 
     end 
     function handlePropEvent(this, src, event) 
      this.childClass = this.childClass; 
     end 

    end 
end 

주 청취자는 childClass 특성에 다른 오브젝트가 지정된 경우 적절하게 처리 (재 지정)됩니다.

실제적으로 childClass은 관심있는 모든 속성 변경에 의해 트리거되는 자체 이벤트 유형을 구현해야하며 parentClass은 해당 이벤트 만 수신합니다.

+0

제안 해 주셔서 감사합니다. 나는 당신의 "실제적인"제안이 갈 길이라고 믿습니다. –