Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

value classes - do you use them? I must be doing something wrong...

조회 수: 2 (최근 30일)
Eric Sampson
Eric Sampson 2013년 5월 18일
마감: MATLAB Answer Bot 2021년 8월 20일
So I can't really recall ever creating a value class, all mine seem to be handle classes. Is that the same as your experience? Unless I'm completely missing something, using value classes seems to be painful, because you always have to do two steps like this:
foo = MyValueClass(5); % sets a property 'val'
foo = foo.double();
plot(foo.val);
With a handle class, I'd just do this:
foo = MyHandleClass(5);
plot(foo.double()); % double returns the new obj.val directly.
Am I completely out to lunch with this understanding of value classes?
As an aside, sometimes it seems like it would be nice to have the above syntax of a handle class, but be able to make independent copies of a given object like a value class... Anyone else think so?
Thanks for your thoughts, Eric

답변 (1개)

Andrew Newell
Andrew Newell 2013년 6월 6일
I have no idea what your method double does or what your class constructor does with the input, so I'll create a trivial example where you could do your plot in two commands:
classdef MyValueClass
properties
val
end
methods
function obj = MyValueClass(val)
obj.val = val;
end
end
end
Now for an example of its use:
foo = MyValueClass(0:1);
plot(foo.val)
See Comparing Handle and Value Classes. For much of the work I do, value classes are preferable.

이 질문은 마감되었습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by