我有一个按句柄的类,我想按值复制它并将其存储在某个地方。但是,如果我更改了课程中的任何内容,它也会更改它的副本。
以下是我要复制的课程示例:
classdef MyClass < handle
properties
data;
end
methods
function M = MyClass()
M.data=5;
end
end
end
以下是测试更改的测试类:
classdef Test < handle
properties
store
end
methods
function N = Test(M)
N.store = M;
end
end
end
现在,我们创建一个MyClass实例并将其存储在Test:
中>> m=MyClass
m =
MyClass handle
Properties:
data: 5
Methods, Events, Superclasses
>> a=Test(m)
a =
Test handle
Properties:
store: [1x1 MyClass]
Methods, Events, Superclasses
>> a.store
ans =
MyClass handle
Properties:
data: 5
Methods, Events, Superclasses
最后,如果我更改'm'中'data'的值,我不希望它改变商店中的值,但是看起来MyClass通过引用存储在'store'中:
>> m.data=3;
>> a.store
ans =
MyClass handle
Properties:
data: 3
Methods, Events, Superclasses
>> a.store.data
ans =
3
是否可以复制“句柄”类?或者我是否需要更改我的课程才能使其按照值生效?