What does the 'Constant' modifier do to cause this behavior?

조회 수: 2 (최근 30일)
Nathan Jessurun
Nathan Jessurun 2018년 7월 17일
댓글: Steven Lord 2018년 7월 17일
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();
}

채택된 답변

Steven Lord
Steven Lord 2018년 7월 17일
From the "Initialize Properties to Unique Values" section on this documentation page:
"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:
  • 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.
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
Nathan Jessurun
Nathan Jessurun 2018년 7월 17일
편집: Nathan Jessurun 2018년 7월 17일
This is a very clear and understandable answer. Thanks!
Regarding the 'Constant' modifier: What does it do regarding 'handle' objects? If all classes were initialized in their constructor but the property was Constant, would the instances still refer to different objects?
Steven Lord
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 CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by