【发布时间】:2012-07-24 00:49:24
【问题描述】:
在 Matlab 类中,同时声明 Dependent(计算未存储)和 Observable 的 property 似乎在语法上是正确的.考虑代码
properties (Access = private)
instanceOfAnotherClass
end
properties (SetAccess = private, Dependent, SetObservable)
propertyTwo
end
methods
function val = get.propertyTwo(this)
val = this.instanceOfAnotherClass.propertyOne;
end
end
这是否按预期工作?也就是说,如果存储在instanceOfAnotherClass 中的对象的属性propertyOne 发生更改,是否有propertyTwo 触发的属性更改事件?请注意,propertyOne 不是可观察的。
编辑:
它不起作用(如我所料)。 'PostSet' 事件未触发。那么我该如何处理这种情况呢?有没有更好的解决方案,然后将 propertyTwo 创建为非 Dependent 并在每次“propertyOne”更改时将其设置为与“propertyOne”相同的值?
编辑2: 针对Amro'sedit of his answer,我将解释更复杂的情况。 考虑这两个类:
classdef AClass < handle
properties
a
end
end
classdef BClass < handle
properties (Access = private)
aClassInst
end
properties (Dependent, SetObservable, SetAccess = private)
b
end
methods
function this = BClass(aClass)
this.aClassInst = aClass;
end
function val = get.b(this)
val = this.aClassInst.a;
end
end
end
使用所有这些代码的类不应访问AClass。它只与BClass 的实例交互,并希望监听b 属性的变化。但是,如果我将a 的AClass 的属性设为可观察的,那将无法解决我的问题,对吗? 'PostSet' 事件不会传播到属性 b,是吗?
【问题讨论】:
-
你为什么不在一个最小的测试例子上测试它?
-
@mutzmatron 好点:)我可以自己测试。但事实上,我希望它不起作用,并想要一些关于如何使它起作用的想法......或者关于这类问题的良好实践。这次我应该编辑我的问题并提出正确的问题。
标签: oop events matlab properties event-handling