Class 属性 函数未设置
Class property not set by function
我正在使用 MATLAB classes,遇到属性问题。
我调用的代码是
theory = ODtheory;
theory.enableDebug(true);
theory.captureRange();
最后一个函数应该打印出值,因为调试设置为 true
这是 class 的代码:
function obj = ODtheory(~)
obj.isDebug = false;
end
function obj = enableDebug(obj,value)
obj.isDebug = value;
end
但在 function range = captureRange(obj)
中,变量 (属性) obj.isDebug
仍设置为 false。
function range = captureRange(obj)
...
if obj.isDebug; disp(['FOW: ' num2str(round(FOW,1)) '/mm']); end
end
我假设你的ODtheory
class是一个value class,所以你需要将它赋值回自己来注册对象属性的变化。
例如:
theory = ODtheory;
theory = theory.enableDebug(true);
theory.captureRange();
如果您的 captureRange
函数也修改对象属性,您也应该使用
调用它
theory = theory.captureRange();
另一种方法是将 class 设为 handle
class(参见上面的 link)
classdef ODtheory < handle
% ... class definition here ...
end
然后你可以在没有分配的情况下使用你的原始代码。
我正在使用 MATLAB classes,遇到属性问题。
我调用的代码是
theory = ODtheory;
theory.enableDebug(true);
theory.captureRange();
最后一个函数应该打印出值,因为调试设置为 true
这是 class 的代码:
function obj = ODtheory(~)
obj.isDebug = false;
end
function obj = enableDebug(obj,value)
obj.isDebug = value;
end
但在 function range = captureRange(obj)
中,变量 (属性) obj.isDebug
仍设置为 false。
function range = captureRange(obj)
...
if obj.isDebug; disp(['FOW: ' num2str(round(FOW,1)) '/mm']); end
end
我假设你的ODtheory
class是一个value class,所以你需要将它赋值回自己来注册对象属性的变化。
例如:
theory = ODtheory;
theory = theory.enableDebug(true);
theory.captureRange();
如果您的 captureRange
函数也修改对象属性,您也应该使用
theory = theory.captureRange();
另一种方法是将 class 设为 handle
class(参见上面的 link)
classdef ODtheory < handle
% ... class definition here ...
end
然后你可以在没有分配的情况下使用你的原始代码。