Bizarre value class behavior
古いコメントを表示
Below is a self-explanatory VALUE class I developed for purposes of illustration.
classdef ClassA
properties
x;
y;
end
methods
function obj = ClassA()
obj.x = 0;
obj.y = 0;
end
function setX(obj, val)
obj.x = val;
end
function setY(obj, val)
obj.y = val;
end
function obj = set.x(obj, val)
*obj* .x = val;
end
function obj = set.y(obj, val)
obj.y = val;
end
end
end
I instantiated it as follows:
a = ClassA();
Good. That worked. Now to test the setter methods:
a.x = 1;
a.y = 2;
Those worked too. But doing this:
a.setX(3)
a.setY(5)
causes the prompt to not echo the value of the properties as is done when the setter methods are used. Why? And when I enter the variable name in the prompt, the property values are echoed back but they do not change upon using setX and setY. This is puzzling. I followed proper VALUE class syntax for setX and setY. Help please!
採用された回答
その他の回答 (2 件)
Image Analyst
2014 年 8 月 31 日
0 投票
They're methods. Not all methods and functions echo stuff to the command window. Some do, for example jet, colormap, etc. Some do not, for example "hold on", "axis off", etc. Note that, while your functions setX() and setY() do something, they don't actually have any return arguments defined . So they won't even put anything into "ans" or return anything whatsoever. Hence nothing will be echoed to the command window, like if they returned an output argument.
5 件のコメント
Image Analyst
2014 年 8 月 31 日
It's been a while since I used a class. I'm surprised that your last two methods even work since they have dots in the method name. I see the setX and setY methods take two input arguments while you're passing only 1 - is that right (I'd have to review)? And did you actually check after you called to see if they were set by putting these lines
% Report values to command window
a.x % Let's see if they actually changed.
a.y
per isakson
2014 年 8 月 31 日
Read my answer
カテゴリ
ヘルプ センター および File Exchange で MATLAB Mobile Fundamentals についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!