Set methods for two interdependent properties in class: how do I avoid infinite recursion?
조회 수: 11 (최근 30일)
이전 댓글 표시
I want to define a class where two properties depend on each other. This means that each set method should modify the other property accordingly.
Simplified example: let's say I define a class for a car, that contains its speed in different units:
classdef Car
properties
speed_km_h = 0;
speed_mph = 0;
end
methods
function obj = set.speed_km_h(obj, value)
obj.speed_km_h = value;
obj.speed_mph = value / 1.61;
end
function obj = set.speed_mph(obj, value)
obj.speed_mph = value;
obj.speed_km_h = value * 1.61;
end
end
end
Implemented like above, asigning a value to one of the properties results in an infinite recursion loop, since each set method calls the other one:
c = Car;
c.speed_km_h = 30;
Out of memory. The likely cause is an infinite recursion within the program.
What's the corrent way to implement this kind of functionality?
Some notes:
- In the simple example above, having one of the values be a dependent property might be a better solution, but I'm asking explicitly about two properties that actually store these values seperatly in the object.
- Defining the properties with AbortSet breaks the recursion and makes the example work, but only if the third setter in the recursion stack tries to set the same value as the original one. If e. g. one of the unit conversions were to introduce a rounding error, the recursion would happen again. There must be a more elegant way, right?
댓글 수: 4
Matt J
2022년 11월 7일
편집: Matt J
2022년 11월 7일
The appropriate recommendation depends on how they will be accessed (read or write) and how often in each case. If they will only be read-accessed often, then you may as well just compute both properties once in the constructor and make them Immutable. No need for set.property methods.
If they need to be write-accessed often, there is no benefit to storing them both. The same computations will be required to keep them consistent whether one of the properties is Dependent or not. In fact, it would be more benefical to have one property Dependent, because then only the concrete property needs to be updated in a write operation.
채택된 답변
Matt J
2022년 11월 7일
편집: Matt J
2022년 11월 7일
Here's a possible solution with both properties Dependent. It will avoid the infinite recursion, but as I said above, it's not clear to me that storing 2 versions of the same property is the wisest course.
classdef Car
properties
Speed_km_h = 0;
Speed_mph = 0;
end
properties (Dependent)
speed_km_h
speed_mph
end
methods
function obj = set.speed_km_h(obj, value)
obj.Speed_km_h = value;
obj.Speed_mph = value / 1.61;
end
function obj = set.speed_mph(obj, value)
obj.Speed_mph = value;
obj.Speed_km_h = value * 1.61;
end
function value = get.speed_km_h(obj)
value =obj.Speed_km_h;
end
function value = get.speed_mph(obj)
value =obj.Speed_mph;
end
end
end
end
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!