如何在Matlab句柄类中创建“单向依赖”

4

我对Matlab还比较陌生,想知道是否有一种方法可以创建“单向句柄类”。

更好地解释一下,假设我有一个名为test_class的类,具有属性“prop1”和“prop2”。

test_1 = test_class(5,10)
test_2 = test_1

我希望对test_1(父级)中的属性进行更改会影响test_2(子级),但我不希望test_2中的更改会影响test_1,因此

test_1.prop1 = 20;
test_2.prop1: 20

test_2.prop2 = 30;
test_1.prop2: 5

有没有一种方法可以创建这样的“单向依赖关系”?

提前感谢!

3个回答

4

您可以通过利用属性集监听器来实现此操作,而不需要涉及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调用展示的一样,或者您可以操作所有属性,并让addlistener调用在任何不是SetObservable的属性上出错。
>> 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

3
这是一个基本实现。每个对象都有一个父对象和一个子对象数组。使用subsasgn,我们可以更改对象及其子对象的属性,因为该对象是单向的,所以不希望更改父对象的属性。
用法:
a = oneway(1,2);
b = oneway(a);
c = oneway(b);

如果我们设置a.prop1 = 7;,那么b将被改变,从而导致c的改变。如果你只想改变直接子元素,你可以取消注释第31行并注释第30行。
classdef oneway < handle
    properties
        parent
        children={};
    end
    properties
     prop1
     prop2
    end
    methods
        function obj = oneway(varargin)
            if nargin == 1
                a = varargin{1};
                if isa(a,'oneway')
                    obj.prop1 = a.prop1;
                    obj.prop2 = a.prop2;
                    obj.parent = a;
                    a.children{end+1} = obj;
                end
            elseif nargin == 2
                obj.prop1 = varargin{1};
                obj.prop2 = varargin{2};
            end
        end
        function obj = subsasgn(self, S, B)
            if strcmp(S.type, '.')
                if ismember(S.subs, properties(self))
                    obj = builtin('subsasgn', self, S, B);
                    for k = 1: numel(self.children)
                        self.children{k} = subsasgn(self.children{k},S,B);
                        %self.children{k} = builtin('subsasgn', self.children{k}, S, B);
                    end
                end
            end
        end
        function delete(self)
            self.parent.children (cellfun(@(x)x==self,self.parent.children))=[];
            for k = 1: numel(self.children)
                self.children{k}.parent =[];
            end
        end
    end
end

0

我认为这是不可能的。您可以拥有引用相同基础对象的句柄对象副本(双向依赖),也可以拥有副本彼此独立的值对象。 对象行为。

您可以创建一个包含句柄的属性的值对象,因此您的对象部分是双向的,部分是单向的。但这并不是您要求的。


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