Matlab OOP 在保存瞬态属性时调用 get 方法。
我正在尝试实现一个类,该类的属性可以提供给构造函数或可能在其他方法中生成。我不希望将数据保存到磁盘或在加载时生成。到目前为止我所遇到的是:
classdef MyClass
properties(GetAccess = public, SetAccess = private)
Property1
Property2
Property3
end
properties(Access = private)
Property4
end
properties(Transient = true)
ProblemProperty
end
properties(Dependent = true, Transient = true)
Property5
end
methods
function MyClass
% Constructor.
end
function val = get.Property5(B)
val = SomeFunction(Property1);
end
function val = get.ProblemProperty(B)
if isempty(B.ProblemProperty)
B = GenerateProblemProperty(B);
end
val = B.ProblemProperty;
end
function B = GenerateProblemProperty(B)
B.ProblemProperty = AnotherFunction(B.Property2);
end
end
end
问题是,当我尝试将对象保存到磁盘时,Matlab 调用 get.ProblemProperty 方法(通过仅在 save 语句上运行探查器来确认)。 ProblemProperty 字段为空,我希望它保持这种状态。它不调用 get.Property5 方法。
如何避免调用 get.ProblemProperty?
I'm trying to implement a class with a property that can be possibly provided to the constructor or possibly generated in some other method. I don't want the data saved to disk or generated on load. What I have so far is:
classdef MyClass
properties(GetAccess = public, SetAccess = private)
Property1
Property2
Property3
end
properties(Access = private)
Property4
end
properties(Transient = true)
ProblemProperty
end
properties(Dependent = true, Transient = true)
Property5
end
methods
function MyClass
% Constructor.
end
function val = get.Property5(B)
val = SomeFunction(Property1);
end
function val = get.ProblemProperty(B)
if isempty(B.ProblemProperty)
B = GenerateProblemProperty(B);
end
val = B.ProblemProperty;
end
function B = GenerateProblemProperty(B)
B.ProblemProperty = AnotherFunction(B.Property2);
end
end
end
The problem is that when I try to save the object to disk, Matlab calls the get.ProblemProperty method (confirmed by running the profiler on just the save statement). The ProblemProperty field is empty and I want it to stay that way. It doesn't call the get.Property5 method.
How do I avoid the call to get.ProblemProperty?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于有时可以设置该属性(即在构造函数中),因此该属性不是严格依赖的。一种解决方案是将可设置值存储在构造函数中的私有属性(下例中的
CustomProblemProperty
)中。然后,ProblemProperty
的get
方法将检查是否返回此私有属性值(如果不为空),否则返回生成的值。Since the property can sometimes be set (i.e. in the constructor) then this property is not strictly dependent. One solution is to store the settable value in a private property (
CustomProblemProperty
in the example below) in the constructor. Theget
method ofProblemProperty
would then check return this private property value if it's not empty and otherwise return the generated value.你的解决方案是有效的,但它不是 OOP 精神,你将对象的外部形状与内部的访问器混合在一起。
我建议以下
NiceProperty 永远不会保存在任何地方,并且您可以受益于编写标准代码。
Your solution is working, but it is not the OOP spirit, you are mixing accessors which are the outside shape of the object with what is inside.
I would advise the following
NiceProperty is then never saved anywhere and you have the benefit of writing standard code.