在 Matlab 中,我可以这样定义一个类:
classdef klass < handle
properties(Dependent)
prop
end
end
Matlab 非常乐意实例化此类的对象,即使没有为 prop
定义 getter。它只有在我尝试访问它时才会失败(可以理解)。我想根据属性名称动态设置 GetMethod
。
不幸的是,即使属性是 Dependent,GetMethod
的 meta.property
字段仍然是只读的。虽然从 dynamicprops
继承可以允许添加一个属性并在每个实例中以编程方式设置其 GetMethod,但我认为它不能用于更改现有属性。我可能不得不走这条路,但由于 prop
必须 存在于每个对象中,所以我更愿意简单地逐个类地设置 getter。这样的事情可能吗?
另一种解决方案可能是通过某种包罗万象的方法。在其他语言中,这可以通过类似 Ruby 的 method_missing
来完成。或类似 PHP 的 __get()
.但据我所知,在 Matlab 中没有(记录或其他)模拟。
(我的用例:这个类被许多用户定义的子类继承,并且它们的所有依赖属性都以类似的方式访问,只是根据属性名称进行更改。而不是要求用户编写 get。 *
方法为它们的每个依赖属性包装对公共(public)代码的调用,我想使用包含必要元数据的匿名函数指针动态设置它们。
最佳答案
这是我的建议:在父类(super class)中创建一个名为 add_dyn_prop
的方法。此方法将在子类中调用,而不是创建 dependent property。通常的方式。
想法是父类(super class)继承自dynamicprops
并使用 addprop
至 add一个新属性,并根据其名称手动设置其访问器方法。
classdef klass < dynamicprops
methods (Access = protected)
function add_dyn_prop(obj, prop, init_val, isReadOnly)
% input arguments
narginchk(2,4);
if nargin < 3, init_val = []; end
if nargin < 4, isReadOnly = true; end
% create dynamic property
p = addprop(obj, prop);
% set initial value if present
obj.(prop) = init_val;
% define property accessor methods
% NOTE: this has to be a simple function_handle (@fun), not
% an anonymous function (@()..) to avoid infinite recursion
p.GetMethod = @get_method;
p.SetMethod = @set_method;
% nested getter/setter functions with closure
function set_method(obj, val)
if isReadOnly
ME = MException('MATLAB:class:SetProhibited', sprintf(...
'You cannot set the read-only property ''%s'' of %s', ...
prop, class(obj)));
throwAsCaller(ME);
end
obj.(prop) = val;
end
function val = get_method(obj)
val = obj.(prop);
end
end
end
end
现在在子类中,我们没有像通常那样定义依赖属性,而是在构造函数中使用这个新的继承函数来定义动态属性:
classdef subklass < klass
%properties (Dependent, SetAccess = private)
% name
%end
%methods
% function val = get.name(obj)
% val = 'Amro';
% end
%end
methods
function obj = subklass()
% call superclass constructor
obj = obj@klass();
% define new properties
add_dyn_prop(obj, 'name', 'Amro');
add_dyn_prop(obj, 'age', [], false)
end
end
end
输出:
>> o = subklass
o =
subklass with properties:
age: []
name: 'Amro'
>> o.age = 10
o =
subklass with properties:
age: 10
name: 'Amro'
>> o.name = 'xxx'
You cannot set the read-only property 'name' of subklass.
当然,现在您可以根据最初的意图自定义基于属性名称的 getter 方法。
编辑:
根据评论,请在下面找到上述相同技术的细微变化。
想法是要求子类创建一个属性(在父类(super class)中定义为抽象),其中包含要创建的所需动态属性的名称。然后父类(super class)的构造函数将创建指定的动态属性,将它们的访问器方法设置为通用函数(可以根据您的请求根据属性名称自定义它们的行为)。我正在重复使用我之前提到的相同的 add_dyn_prop
函数。
在子类中,我们只需要实现继承的抽象 dynamic_props
属性,使用名称列表(或者 {}
如果您不想创建任何初始化动态属性)。例如我们写:
classdef subklass < klass
properties (Access = protected)
dynamic_props = {'name', 'age'}
end
methods
function obj = subklass()
obj = obj@klass();
end
end
end
父类(super class)与我们以前的类似,只是现在它有责任在其构造函数中为每个属性名称调用 add_dyn_prop
:
classdef klass < dynamicprops % ConstructOnLoad
properties (Abstract, Access = protected)
dynamic_props
end
methods
function obj = klass()
assert(iscellstr(obj.dynamic_props), ...
'"dynamic_props" must be a cell array of strings.');
for i=1:numel(obj.dynamic_props)
obj.add_dyn_prop(obj.dynamic_props{i}, [], false);
end
end
end
methods (Access = private)
function add_dyn_prop(obj, prop, init_val, isReadOnly)
% input arguments
narginchk(2,4);
if nargin < 3, init_val = []; end
if nargin < 4, isReadOnly = true; end
% create dynamic property
p = addprop(obj, prop);
%p.Transient = true;
% set initial value if present
obj.(prop) = init_val;
% define property accessor methods
p.GetMethod = @get_method;
p.SetMethod = @set_method;
% nested getter/setter functions with closure
function set_method(obj,val)
if isReadOnly
ME = MException('MATLAB:class:SetProhibited', sprintf(...
'You cannot set the read-only property ''%s'' of %s', ...
prop, class(obj)));
throwAsCaller(ME);
end
obj.(prop) = val;
end
function val = get_method(obj)
val = obj.(prop);
end
end
end
end
注意:我没有使用 ConstructOnLoad
类属性或 Transient
property 属性,因为我仍然不确定它们会如何影响从保存的 MAT 文件加载对象的动态属性。
>> o = subklass
o =
subklass with properties:
age: []
name: []
>> o.name = 'Amro'; o.age = 99
o =
subklass with properties:
age: 99
name: 'Amro'
关于matlab - 在 MATLAB 中为依赖属性动态分配 getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18258805/