【发布时间】:2015-07-21 08:25:07
【问题描述】:
我有一个带有“下一个状态”属性的 matlab 类。
我在类中定义了以下函数:
function obj = decideNextAction(obj)
obj.current_patch_quality
% Important, rand is redeclared in the two calls. So it may be
% that rand < current_patch_quality in the first if and greater
% than in the second if
if(rand > obj.current_patch_quality)
obj.next_action = 1;
elseif(rand < obj.current_patch_quality)
obj.next_action = 3;
else
obj.next_action = 2;
end
end
应该将实例属性重新定义为 1、2 或 3。但是,我做了一些试验,似乎该函数返回了一个带有修改后实例的新对象,但没有修改原始对象。有什么建议吗?
>> x = recruit([0 0])
x =
recruit with properties:
nest_location: [0 0]
current_patch_location: []
current_patch_quality: 0
next_action: []
>> x.decideNextAction
ans =
0
ans =
recruit with properties:
nest_location: [0 0]
current_patch_location: []
current_patch_quality: 0
next_action: 1
>> x
x =
recruit with properties:
nest_location: [0 0]
current_patch_location: []
current_patch_quality: 0
next_action: []
>>
【问题讨论】: