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

有没有办法创造这种“单向依赖”?

提前致谢!

4

3 回答 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

请注意,这要求属性SetObservableSetObservable排除任何不是的属性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 
于 2017-10-26T16:28:23.397 回答
3

这是一个基本的实现。每个对象都有一个父对象和一组子对象。使用subsasgn我们可以更改对象及其子对象的属性,并且由于对象是单向的,我们不想更改父对象的属性。

用法:

a = oneway(1,2);
b = oneway(a);
c = oneway(b);

如果我们设置a.prop1 = 7;thenb将会改变导致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
于 2017-10-23T20:09:18.233 回答
0

我不认为这是可能的。您可以拥有引用相同底层对象(依赖于 2 路)的句柄对象的副本,也可以拥有副本相互独立的值对象。 对象行为。

您可能能够创建具有Property包含句柄的值对象......所以您的对象的一部分是 2 路的,部分是 1 路的。但这并不是你真正要求的。

于 2017-10-23T17:51:08.320 回答