我创建自己的类如下:
classdef testClass < handle
properties
value;
map = containers.Map('KeyType','double','ValueType','any');
end
end
Run Code Online (Sandbox Code Playgroud)
我的目标是为每个对象testClass
维护自己的地图.然而,事实证明,整个类只有一个地图对象:testClass
访问相同的所有对象containers.Map
.例如,如果我创建两个对象,如下所示
a = testClass;
b = testClass;
a.value = 'a';
b.value = 'b';
a.map(1) = 123;
b.map(2) = 321;
Run Code Online (Sandbox Code Playgroud)
它最终都是两个,a
并且b
地图包含两个键值对:
>> a
a =
testClass handle
Properties:
value: 'a'
map: [2x1 containers.Map]
>> b
b =
testClass handle
Properties:
value: 'b'
map: [2x1 containers.Map]
Methods, Events, Superclasses
Run Code Online (Sandbox Code Playgroud)
两个(键,值)对(1,123)和(2,321)都出现在a.map
和b.map
>> a.map.keys
ans =
[1] [2]
>> …
Run Code Online (Sandbox Code Playgroud)