assigning to object variables from within-object functions

조회 수: 23 (최근 30일)
Michel du Montmorency
Michel du Montmorency 2015년 7월 6일
편집: Luke Perry 2019년 7월 3일
Can anyone explain me the following code, please?
myObj = myClass() ;
newObj = myObj.Increment() ;
disp myObj.Value
disp newObj.Value
myClass is defined as follows:
classdef myClass
properties
Value
end
methods
function self = myClass(self)
self.Value = 5 ;
end
function self = Increment(self)
self.Value = self.Value + 1 ;
end
end
end
Here myObj.Value is always 5, and it is not incremented to 6. In all the other languages I know, there is no need to explicitely write
newObj = oldObj.Function()
in order to make actions of .Function effective on the object itself. Is MATLAB working in that way, or there is something I am missing?
Thank you all,
Mike

채택된 답변

David Young
David Young 2015년 7월 6일
MATLAB always makes a copy when you assign an object, or pass an object to a function. Here's another example of the same thing, but with a simple array:
a = [1 2 3];
b = a;
b(2) = 99;
disp(a)
which prints
1 2 3
- that is, a has not changed. In just the same way, myObj has been copied and not changed in your code. In general, to change the value of an element of an array or a property of an object, the name of the array or object must appear on the left side of an assignment.
Although this is different to some languages, it's an enormously valuable aspect of MATLAB, and must have saved vast amounts of programmer time over the years. It's consistent, and it entirely avoids a significant class of bugs.
This sounds inefficient, but it isn't - the copy is only made when it's actually necessary.
You can override the default and have the behaviour you are used to by declaring your class to be a handle class. For consistency with the rest of the language this is best avoided, but there are situations where a handle class is called for. For more details see this part of the documentation.

추가 답변 (1개)

Brendan Hamm
Brendan Hamm 2015년 7월 6일
편집: Brendan Hamm 2015년 7월 6일
If you want to have your original object updated you should make your class a handle class:
classdef myClass < handle
properties
Value
end
methods
function self = myClass()
self.Value = 5 ;
end
function Increment(self)
self.Value = self.Value + 1 ;
end
end
end
By default MATLAB is using a pass by value behavior, so you are passing in your object as an argument to a method, but getting back a different object. Handle classes are more akin to classes in other languages and have a pass by reference behavior.
mc = myClass
mc.Increment
mc.Value
ans =
6
  댓글 수: 2
Michel du Montmorency
Michel du Montmorency 2015년 7월 6일
Thank you, that was clarifying!
Luke Perry
Luke Perry 2019년 7월 3일
편집: Luke Perry 2019년 7월 3일
Thank you Brendan. This was very helpful. However, is there any reason for creating another instance of the object and passing it back through?
From what I understand of MATLAB, due to problems in debugging, it is not good to set an object equal to itself such as the following:
myclass=New_Class();
myclass=myclass.SetColor('yellow');
myclasscolor=myclass.GetColor();
where the class is defined below:
classdef New_Class
properties (Access = protected)
color;
end
methods (Access = public)
function obj = New_Class(obj)
obj.color='';
end
end
methods
function obj = SetColor(obj,color)
obj.color=color;
end
function color = GetColor(obj)
color=obj.color;
end
end
end
Why then would the default class be encouraged to act this way? I should be able to just do:
myclass=New_Class();
myclass.SetColor('yellow');
myclasscolor=myclass.GetColor();
Otherwise this seems like a poor way of going about Object-Oriented Programming.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Handle Classes에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by