仅在我的程序上添加getter / setter方法会使我的程序超级慢。有什么解释吗?
function set.x(obj,newx)
obj.x = newx;
end
function x = get.x(obj)
x = obj.x;
end
这就是我在handle类下定义它们的方式。
还是我只是没有正确实现它们?
编辑
类定义去了...
classdef sensorlocest < handle
properties(GetAcess = 'public', SetAccess = 'private')
sensorId; % sensor id
X; % true x-coordainate
Y; % true y-coordinate
x; % estimate of X
y; % estimate of Y
end
methods
function sesnors = sensorlocest(x,y)
if nargin ~= 0
sesnors(49,1) = sensorlocest;
for k = 1:length(sensors)
sensors(k).sesnorId = k;
sensors(k).X = x.*rand;
sensors(k).Y = y.*rand;
end
end
function init(sensors,x,y)
N = length(sensors);
for i = 1:N
sensors(i).x = x.*rand;
sensors(i).y = y.*rand;
end
end
function set.x(sensors,newx)
sensors.x = newx;
end
function set.y(sensors,newy)
sensors.y = newy;
end
end
end
最佳答案
不,您是正确的。他们确实有开销,请查看以下线程:
Is MATLAB OOP slow or am I doing something wrong?