Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab:对句柄对象的引用_Matlab_Pointers_Reference_Pass By Reference - Fatal编程技术网

Matlab:对句柄对象的引用

Matlab:对句柄对象的引用,matlab,pointers,reference,pass-by-reference,Matlab,Pointers,Reference,Pass By Reference,我是否可以创建一个句柄对象的引用,以便在某一点替换对象本身,并且引用将被更新 例如: classdef IShifter < handle methods (Abstract) x = Shift(this, x); end end classdef Shifter1 < IShifter methods function x = Shift(this, x) x = circshift(x, 1); end end end c

我是否可以创建一个句柄对象的引用,以便在某一点替换对象本身,并且引用将被更新

例如:

classdef IShifter < handle
  methods (Abstract)
    x = Shift(this, x);
  end
end

classdef Shifter1 < IShifter
  methods
    function x = Shift(this, x)
      x = circshift(x, 1);
    end
  end
end

classdef Shifter2 < IShifter
  methods
    function x = Shift(this, x)
      x = [ 0 ; x ];
    end
  end
end



classdef Item
  properties (Access = 'private')
    shifter; % should be a pointer/reference to the object which is in the respective parent container object
  end

  methods
    function this = Item(shifter)
      this.shifter = shifter;
    end

    function x = test(this, x)
      x = this.shifter.Shift(x);
    end
  end
end

% note this is a value class, NOT a handle class!
classdef ItemContainer
  properties
     shifter;
     items;
  end

  methods
    function this = ItemContainer()
      this.shifter = Shifter1;
      this.items{1} = Item(this.shifter);
      this.items{2} = Item(this.shifter);
    end

    function Test(this)
      this.items{1}.Test( [ 1 2 3] )
      this.items{2}.Test( [ 1 2 3] )
    end
  end
end
但事实是:

items = ItemContainer();
items.Test();
[ 3 1 2 ]
[ 3 1 2 ]
items.shifter = Shifter2;
items.Test();
[ 3 1 2 ]
[ 3 1 2 ]
因为将新的移位器对象指定给父对象项不会更新容器中的引用

我正在寻找类似于C语言的东西,其中所有的“移位器”属性都是指针,我可以将任何我想要的移位器对象放入这个“地址”

ItemContainer和Item不是句柄类。
我希望避免使用事件来更新引用,或者实现一个set方法来更新引用。Matlab中的pass-by-reference概念仅限于此(而且主要限于
handle
-类)。它没有达到你想要的程度

如果不想使用set方法,可以使用返回
{this.shifter,this.shifter}


根据您的评论,
比一个单元格数组要复杂一些。因此,您可能希望使用属性
itemsStore
(或
itemsCache
,或临时存储
项的任何名称)和从属属性
项来设置对象。
items
的get方法检查
itemsStore
是否为空;如果是,它将重建项目数组并将其存储在
itemsStore
中,如果不是,它只返回
itemStore
的内容。此外,您需要向
移位器
添加一个set方法来清空
项存储
,以便需要重新创建
。请注意,MLint将给您一个警告,即属性的set方法不应写入另一个属性。此警告旨在告诉您,当您保存了一个对象,然后再次从磁盘加载该对象时,将执行所有set方法。根据执行顺序,写入其他属性的set方法可能会产生意外结果。在您的情况下,这没有问题,因为清空的
itemStore
是您的代码设计用来处理的东西。如果需要,可以右键单击MLint警告,禁用该行的警告。

Matlab中的“通过引用”概念仅限于此(并且主要限于
句柄
-类)。它没有达到你想要的程度

如果不想使用set方法,可以使用返回
{this.shifter,this.shifter}


根据您的评论,
比一个单元格数组要复杂一些。因此,您可能希望使用属性
itemsStore
(或
itemsCache
,或临时存储
项的任何名称)和从属属性
项来设置对象。
items
的get方法检查
itemsStore
是否为空;如果是,它将重建项目数组并将其存储在
itemsStore
中,如果不是,它只返回
itemStore
的内容。此外,您需要向
移位器
添加一个set方法来清空
项存储
,以便需要重新创建
。请注意,MLint将给您一个警告,即属性的set方法不应写入另一个属性。此警告旨在告诉您,当您保存了一个对象,然后再次从磁盘加载该对象时,将执行所有set方法。根据执行顺序,写入其他属性的set方法可能会产生意外结果。在您的情况下,这没有问题,因为清空的
itemStore
是您的代码设计用来处理的东西。如果需要,可以右键单击MLint警告,禁用该行的警告。

不幸的是,我认为这在Matlab中是不可能的

但是,当定义了
item
时,可以使用set方法或重新定义
items{1}
items{2}
的内容。然后你就能得到你想要的行为


最好的,

不幸的是,我认为这在Matlab中是不可能的

但是,当定义了
item
时,可以使用set方法或重新定义
items{1}
items{2}
的内容。然后你就能得到你想要的行为


最好的,

那太可悲了。但是好的,在最坏的情况下,我可以使用
get.shifter(这个,shifter)
并更新所有项目。但是:“非依赖属性的set方法不应访问另一个属性”。我不想使项目依赖,因为这只是一个玩具的例子。实际上,我的对象要复杂得多。。。建议?使用
set.shifter
的另一个未清理问题:
属于
IItem
(接口)类型,但并非所有可能的项都具有属性
shifter
。如果对象具有,则在构造函数中指定它。因此,在盲目更新列表中的所有项对象之前,
set.shifter
方法需要检查类型(或者属性是否存在)。不太优雅:(@divB:我明白了。在这种情况下,items的get方法会更优雅,因为它会根据需要收集所有当前元素。您能否快速阐述一下?您的意思是将
items
定义为依赖项,并在
get
方法中动态创建循环中的对象吗?这会起作用。但是,
items
在循环中会被频繁访问,在每次读取访问时重新创建整个对象层次结构可能效率很低。或者你是说其他什么?@divB:的确,值类会使这变得更复杂。但是只有一点:创建一个方法
getItems
,该方法同时返回项和更新的对象,而不是
items = ItemContainer();
items.Test();
[ 3 1 2 ]
[ 3 1 2 ]
items.shifter = Shifter2;
items.Test();
[ 3 1 2 ]
[ 3 1 2 ]