- All the innermost_inst properties of the Inner object are references to the same Innermost handle object.
- All the inner_inst properties of Outer1 handle objects are references to the same Inner handle object.
- All inner_inst properties of Outer2 handle objects are references to the same Inner handle object.
- But the inner_inst properties of Outer1 handle objects are not the same object as the inner_inst properties of Outer2 handle objects.
What does the 'Constant' modifier do to cause this behavior?
3 次查看(过去 30 天)
显示 更早的评论
For the following classes:
classdef Outer1 < handle
properties (Constant)
inner_inst = Inner();
end
end
classdef Outer2 < handle
properties (Constant)
inner_inst = Inner();
end
end
classdef Inner < handle
properties
innermost_inst = Innermost();
end
end
classdef Innermost < handle
properties (Constant)
innermost_int = 5;
end
end
I have instantiated variables as follows:
out1a = Outer1();
out1b = Outer1();
out2a = Outer2();
out2b = Outer2();
Then I tried these comparisons (with the result shown next to it):
out1a.inner_inst == out1b.inner_inst: true
out1a.inner_inst == out2b.inner_inst: false
out1a.inner_inst.innermost_inst == out1b.inner_inst.innermost_inst: true
out1a.inner_inst.innermost_inst == out2b.inner_inst.innermost_inst: true
I understand why the top three lines produce their results, but what causes the last line to be true? This same code but with static members instead of Constant produces false for this last comparison in Java.
public class Outer1 {
public static Inner inner_inst = new Inner();
}
0 个评论
采纳的回答
Steven Lord
2018-7-17
"MATLAB assigns properties to the specified default values only once when MATLAB loads the class definition. Therefore, if you initialize a property value with a handle-class constructor, MATLAB calls this constructor only once and every instance references the same handle object. If you want a property value to be initialized to a new instance of a handle object each time you create an object, assign the property value in the constructor." Because of this:
Now what does the == operator do for handle classes? As stated in the "Determining Equality of Objects" section of this documentation page, "Equality for handle objects means that the handle variables refer to the same object." That's why asking if the two innermost_inst properties are == returns true, while asking if the two inner_inst properties are == returns false for an instance of Outer1 and an instance of Outer2.
If you want all four of those comparisons to return true, you want to use isequal.
isequal(out1a.inner_inst, out1b.inner_inst)
isequal(out1a.inner_inst, out2b.inner_inst)
isequal(out1a.inner_inst.innermost_inst, out1b.inner_inst.innermost_inst)
isequal(out1a.inner_inst.innermost_inst, out2b.inner_inst.innermost_inst)
If you want each instance of Outer1 to have a different instance of Inner as its inner_inst property, set that property in the Outer1 constructor. Similarly, if you want each instance of Inner to have a different Innermost as its innermost_inst property, set that in the Inner constructor.
2 个评论
Steven Lord
2018-7-17
You can't assign a value to a Constant property in the constructor. Instead specify the SetAccess attribute of that property to 'immutable'. See this documentation page for a description of the values the SetAccess attribute can have.
classdef Outer1 < handle
properties (SetAccess='immutable')
inner_inst
end
methods
function obj = Outer1
obj.inner_inst = Inner();
end
end
end
Let's check that the inner_inst properties of two instances of this Outer1 class are different objects.
>> y = Outer1;
>> z = Outer1;
>> y.inner_inst == z.inner_inst
ans =
logical
0
As for how the Constant property attribute interacts with handle objects, you can't change the property but you can change properties of the property. See the "Constant Property Assigned a Handle Object" section on this documentation page for an example.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!