Dependent properties vs functions
10 次查看(过去 30 天)
显示 更早的评论
Suppose I have a class that represents rectangles, such that it contains the stored properties 'length' and 'width', and I wish to find the area ... it seems to me that I can define it either as a dependent property (and use the get method to get the area), or as a function (which will return the area).
What is the difference between these two alternatives? After all, to calculate the area for a rectangle object (called rect, say), we would use the same syntax, namely: rect.area
Thanks in advance for your help :-)
0 个评论
回答(2 个)
Vincent MISTLER
2020-7-19
编辑:Vincent MISTLER
2020-7-19
The question is from 2012 but I thought the answer could be useful to other Matlab users.
I don't have a definitive answer to your question because I don't know exactly what is "under the hood" of the Dependent keyword. However, I have found a case where Dependent properties can be useful.
Both methods and dependent properties allow to make computations "on the fly", instead of storing the values into the object. However, replacing a property by a method changes the behavior of the class. For example, you cannot use indexing on that "property".
See an example to illustrate this below:
I create a class Vector to make various computations on an array of double. One of this computation simply computes the square of the vector.
I implement this first using properties only (Vector0.m), then using a method for computing the square (Vector1.m), and finally using a Dependent property (Vector2.m)
Vector0.m: Using properties only
classdef Vector0 < handle
properties
x
squared
end
methods
function this=Vector0(values)
this.x=values;
this.squared=values.^2;
end
end
end
Vector1.m: Using a method to compute the square
classdef Vector1 < handle
properties
x
end
methods
function this=Vector1(values)
this.x=values;
end
function squared=squared(this)
squared=(this.x).^2;
end
end
end
Vector2.m: Using a Dependent Property to compute the square
classdef Vector2 < handle
properties
x
end
properties (Dependent)
squared
end
methods
function this=Vector2(values)
this.x=values;
end
function squared=get.squared(this)
squared=(this.x).^2;
end
end
end
Now, look how the 3 classes behave differently when using disp() and indexing:
Vector0.m: Using properties only
>> v0=Vector0(1:3)
v0 =
Vector0 with properties:
x: [1 2 3]
squared: [1 4 9]
>> v0.squared
ans =
1 4 9
>> v0.squared(end)
ans =
9
squared is a property therefore it is an array of doubles. You can index this array like any other array in Matlab and it works as expected.
Vector1.m: Using a method to compute the square
>> v1=Vector1(0:3)
v1 =
Vector1 with properties:
x: [0 1 2 3]
>> v1.squared
ans =
0 1 4 9
>> v1.squared(end)
Error using Vector1/squared
Too many input arguments.
squared is a method now which returns an array of doubles.
Although you can get the full array by calling the method squared, trying to get the last element by using squared(end) returns an error. Indeed, the method squared does not accept any input argument. Of course, you could add an extra argument to the squared method to allow indexing, but indexing would not accept end as input. Therefore, by replacing the property by a method, the class no longer behaves exactly the same way as before. The code that was using the class and making indexing on class properties you replaced by methods is now broken.
Vector2.m: Using a Dependent Property to compute the square
>> v2=Vector2(1:3)
v2 =
Vector2 with properties:
x: [1 2 3]
squared: [1 4 9]
>> v2.squared
ans =
1 4 9
>> v2.squared(end)
ans =
9
Now squared is a Dependent property. And it behaves exactly like a normal property (you can index it for example)
Hope this helps
0 个评论
Yvo Putter
2018-1-22
For this specific example I still don't know, I also struggled with it.
In general, what I've found is that the get method doesn't allow additional input variables other than the object itself. Also, most find it better to keep the get method as short and simple as possible. Lastly, the use of (Dependent) vs. (SetAccess = private) properties is discussed in this thread.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!