Matlab:引用句柄对象
Matlab: Reference to a handle object
我能否以一种可以一次性替换对象本身并更新引用的方式创建对句柄对象的引用?
示例:
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();
[ 0 1 2 ]
[ 0 1 2 ]
但它是:
items = ItemContainer();
items.Test();
[ 3 1 2 ]
[ 3 1 2 ]
items.shifter = Shifter2;
items.Test();
[ 3 1 2 ]
[ 3 1 2 ]
因为将新的 Shifter 对象分配给父对象项不会更新容器中的引用。
我正在寻找类似 C 的东西,其中所有 "shifter" 属性都是指针,我可以将我想要的任何 Shifter 对象放入此 "address".
ItemContainer 和 Item 不是句柄 类。
我想避免使用事件来更新引用或实现 set 方法来更新引用
Matlab 中的按引用传递概念仅适用于此(并且主要限于 handle
-类)。它并没有达到你想要的程度。
如果您不想使用 set 方法,您可以使用 returns {this.shifter,this.shifter}
根据您的评论,items
比元胞数组要复杂一些。因此,您可能希望使用 属性 itemsStore
(或 itemsCache
,或任何您喜欢的临时存储 items
的名称)和依赖项来设置您的对象属性items
。 items
的 get 方法检查 itemsStore
是否为空;如果是,它重建项目数组并将其存储在 itemsStore
中,如果不是,它只是 returns itemStore
的内容。此外,您需要向 shifter
添加一个清空 itemsStore
的设置方法,因此需要重新创建 items
。请注意,MLint 会警告您 属性 的 set 方法不应写入另一个 属性。这个警告是想告诉你,当你保存了一个对象然后从磁盘再次加载它时,所有的设置方法都会被执行。根据执行顺序,写入其他属性的设置方法可能会产生意外结果。在您的情况下,这没问题,因为清空的 itemStore
是您的代码旨在处理的内容。如果需要,您可以右键单击 MLint 警告,为该行禁用它。
不幸的是,我认为这在 Matlab 中是不可能的。
但是,在定义item
时,可以使用set方法或subsasgn
重新定义items{1}
和items{2}
的内容。然后您将能够获得您要求的行为。
最佳,
我能否以一种可以一次性替换对象本身并更新引用的方式创建对句柄对象的引用?
示例:
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();
[ 0 1 2 ]
[ 0 1 2 ]
但它是:
items = ItemContainer();
items.Test();
[ 3 1 2 ]
[ 3 1 2 ]
items.shifter = Shifter2;
items.Test();
[ 3 1 2 ]
[ 3 1 2 ]
因为将新的 Shifter 对象分配给父对象项不会更新容器中的引用。
我正在寻找类似 C 的东西,其中所有 "shifter" 属性都是指针,我可以将我想要的任何 Shifter 对象放入此 "address".
ItemContainer 和 Item 不是句柄 类。 我想避免使用事件来更新引用或实现 set 方法来更新引用
Matlab 中的按引用传递概念仅适用于此(并且主要限于 handle
-类)。它并没有达到你想要的程度。
如果您不想使用 set 方法,您可以使用 returns {this.shifter,this.shifter}
根据您的评论,items
比元胞数组要复杂一些。因此,您可能希望使用 属性 itemsStore
(或 itemsCache
,或任何您喜欢的临时存储 items
的名称)和依赖项来设置您的对象属性items
。 items
的 get 方法检查 itemsStore
是否为空;如果是,它重建项目数组并将其存储在 itemsStore
中,如果不是,它只是 returns itemStore
的内容。此外,您需要向 shifter
添加一个清空 itemsStore
的设置方法,因此需要重新创建 items
。请注意,MLint 会警告您 属性 的 set 方法不应写入另一个 属性。这个警告是想告诉你,当你保存了一个对象然后从磁盘再次加载它时,所有的设置方法都会被执行。根据执行顺序,写入其他属性的设置方法可能会产生意外结果。在您的情况下,这没问题,因为清空的 itemStore
是您的代码旨在处理的内容。如果需要,您可以右键单击 MLint 警告,为该行禁用它。
不幸的是,我认为这在 Matlab 中是不可能的。
但是,在定义item
时,可以使用set方法或subsasgn
重新定义items{1}
和items{2}
的内容。然后您将能够获得您要求的行为。
最佳,