속성 집합 수신기를 사용하여 subasgn의 어려움없이이 작업을 수행 할 수 있습니다. 이렇게하면 모든 자녀 사본을 가지고 관리하는 업무를 수행 할 필요가 없습니다. 이것은 다음과 같이 보입니다 :
classdef test_class < matlab.mixin.Copyable
properties(SetObservable)
prop1
prop2
end
properties
prop3
end
methods
function obj = test_class(in1, in2, in3)
obj.prop1 = in1;
obj.prop2 = in2;
obj.prop3 = in3;
end
function ref = make_dependent_reference(obj)
ref = copy(obj);
cls = metaclass(obj);
observableProps = cls.PropertyList.findobj('SetObservable',true);
for ct =1:numel(observableProps)
obj.addlistener(observableProps(ct).Name, 'PostSet', ...
@(prop,evd)ref.update_dependent_reference(prop,evd));
end
end
end
methods(Access=private)
function update_dependent_reference(ref, prop, evd)
ref.(prop.Name) = evd.AffectedObject.(prop.Name);
end
end
end
참고,이 SetObservable
, 당신은 그 속성에 대한 참조 업데이트 내가 함께 위에 표시된 것 같은 SetObservable
을하지 않은 속성을 무시하도록 선택할 수 있습니다로 등록이 필요 findobj 호출을 사용하거나 대신 모든 등록 정보에서 작업하고 SetObservable
이 아닌 등록 정보에 대해 addlistener가 오류를 호출하도록 할 수 있습니다.
>> t = test_class(5,10,15)
t =
test_class with properties:
prop1: 5
prop2: 10
prop3: 15
>> ref = t.make_dependent_reference
ref =
test_class with properties:
prop1: 5
prop2: 10
prop3: 15
>> ref.prop1 = 6
ref =
test_class with properties:
prop1: 6
prop2: 10
prop3: 15
>> t
t =
test_class with properties:
prop1: 5
prop2: 10
prop3: 15
>> t.prop2 = 11
t =
test_class with properties:
prop1: 5
prop2: 11
prop3: 15
>> ref
ref =
test_class with properties:
prop1: 6
prop2: 11
prop3: 15
매우 우아합니다! – rahnema1