defining properties in a class
显示 更早的评论
I have the following class
classdef classname
properties (Constant)
Number=1
end
end
I can change the value of `Number`
classname.Number=2
How can I defined properties so that one cannot change the value of the variables defined in `properties`?
回答(1 个)
This doc page seems to suggest this is not natively possible. However, you could implement it as method:
%(untested code)
classdef classname
properties (Private)
%use a struct to store the defaults to keep all your static
%properties in one place
static.Number=1;
end
methods
function val=Number(obj)
val=obj.static.Number;
end
end
end
13 个评论
Walter Roberson
2021-8-3
编辑:Walter Roberson
2021-8-3
I disagree that that link says it is not natively possible; the link says specifically,
"In MATLAB, classes can define constant properties, but not "static" properties in the sense of other languages like C++. You cannot change constant properties from the initial value specified in the class definition."
So classname.Number=2 would be expected to be an error after the initialization.
That was my expectation as well, but given that OP implied this was allowed, I assumed this was the correct interpretation.
But you are correct: this already results in an error (while my code doesn't).
% classdef classname
% properties (Constant)
% Number=1
% end
% end
a=classname;
a.Number=2
Deepa Maheshvare
2021-8-3
编辑:Deepa Maheshvare
2021-8-3
Rik
2021-8-3
classname isn't an object of your class anymore, it has become a struct.
Walter Roberson
2021-8-3
Re-read what Rik pointed to, https://www.mathworks.com/help/matlab/matlab_oop/matlab-vs-other-oo-languages.html#buwskub
in which the description specifically says that
A.B = C
will replace A with a struct in the case that A is a class name .
It does not do that if A is an object of a class, only when A is the name of a class.
It is a bit strang that you normally get a warning when converting a class to a struct, but not with this syntax. Maybe worth an enhancement request.
classname.Number=2 % no warning
struct(line)
Deepa Maheshvare
2021-8-3
编辑:Deepa Maheshvare
2021-8-3
If your value isn't the same for every instance of your class, doesn't it make more sense to use something similar to what I suggested, but use a persistent variable in a method to store a specific value?
It is a bit confusing to me what your end goal is with what you're doing.
Your edit did not clear up the confusion for me.
Also, you should not leave comments if they are no longer true, they will only serve to confuse.
Deepa Maheshvare
2021-8-3
Rik
2021-8-3
That sounds like you need a normal property, not a constant.
Deepa Maheshvare
2021-8-3
Why would it? This is what it returns with your classdef.
I don't understand why you instist on calling things static when they aren't.
main(10)
function main(arg)
classname.fun1(arg,arg)
end
Deepa Maheshvare
2021-8-3
类别
在 帮助中心 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!