フィルターのクリア

Unexpected behavior in classdefs that inherit from handle

1 回表示 (過去 30 日間)
Roger Rouse
Roger Rouse 2023 年 9 月 27 日
コメント済み: Roger Rouse 2023 年 9 月 28 日
I have a simple class, SomeClass, that inherits from handle.
I have another simple class, SomeOtherClass, that has a properity that is a SomeClass object.
When I instantiate two SomeOtherClass and set the value of the SomeClass property then both SomeOtherClass objects receive the value. I assume this is because both SomeOtherClass objects point to the same SomeClass object. But why? The two SomeOtherClass are separate instantiations. Please help me understand. The included code demostrates what I'm talking about.
Thanks, Roger
O1 = SomeClass;
O2 = SomeClass;
O3 = SomeOtherClass;
O4 = SomeOtherClass;
fprintf(1,'\nProperty Values After Instantiation\n');
Property Values After Instantiation
worker(O1,O2,O3,O4);
O1.AProperty = 0.000000 O2.AProperty = 0.000000 O3.PropFromAbove.AProperty = 0.000000 O3.SomeOtherProp = 0.000000 O4.PropFromAbove.AProperty = 0.000000 O4.SomeOtherProp = 0.000000
O2.AProperty = pi;
fprintf(1,'\nProperty Values After Setting O2.AProperity to pi.\n');
Property Values After Setting O2.AProperity to pi.
worker(O1,O2,O3,O4);
O1.AProperty = 0.000000 O2.AProperty = 3.141593 O3.PropFromAbove.AProperty = 0.000000 O3.SomeOtherProp = 0.000000 O4.PropFromAbove.AProperty = 0.000000 O4.SomeOtherProp = 0.000000
O3.PropFromAbove.AProperty = exp(1);
fprintf(1,'\nProperty Values After Setting O3.PropFromAbove.AProperity to exp(1).\n');
Property Values After Setting O3.PropFromAbove.AProperity to exp(1).
worker(O1,O2,O3,O4);
O1.AProperty = 0.000000 O2.AProperty = 3.141593 O3.PropFromAbove.AProperty = 2.718282 O3.SomeOtherProp = 0.000000 O4.PropFromAbove.AProperty = 2.718282 O4.SomeOtherProp = 0.000000
fprintf(1,'\n!!! Why are O3.PropFromAbove and O4.PropFromAbove pointing to the same object!!!\n');
!!! Why are O3.PropFromAbove and O4.PropFromAbove pointing to the same object!!!
function worker(O1,O2,O3,O4)
parm = {'O1','O2'};
objs = {O1,O2};
for k = 1:numel(objs)
fprintf(1,'%s.AProperty = %f\n',parm{k},objs{k}.AProperty);
end
parm = {'O3','O4'};
objs = {O3,O4};
for k = 1:numel(objs)
fprintf(1,'%s.PropFromAbove.AProperty = %f\n',parm{k},objs{k}.PropFromAbove.AProperty);
fprintf(1,'%s.SomeOtherProp = %f\n',parm{k},objs{k}.SomeOtherProp);
end
end

採用された回答

Walter Roberson
Walter Roberson 2023 年 9 月 27 日
This is documented.
MATLAB evaluates a default expression when the property value is first needed (for example, when the class is first instantiated). The same default value is then used for all instances of a class. MATLAB does not reevaluate the default expression unless the class definition is cleared from memory.
So when you initialize a property to an expression that yields a handle, then the same handle will be used for all instances of the class.
If this is not what you want the you need to put in a constructor: constructors are run each time an object of the class is created.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeScopes and Data Logging についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by