arguments
功能存在问题,如果将[[any名称/值参数对传递给子类,则子类无法实例化。
arguments
]功能在简化参数验证和从函数arguments
中删除样板代码方面带来了广阔的前景。但是,在尝试实现从[1]提取的name-value
public
class properties甚至在执行任何子类代码之前。该消息令人困惑,因为制表符补全似乎可以正常地用于NV对:
public
此外,如果根本不传递任何参数,则一切正常:
Invalid argument list. Check for wrong number of positional arguments or placement of positional arguments after name-value pairs. Also, check for name-value pairs with invalid names or not specified in pairs.
vs。
问题:
为什么会出现错误?我不认为我以某种意想不到的或未记录的方式使用
>> FluidLayer() ans = FluidLayer with properties: prop1: NaN prop2: NaN
机制,因此我对自己可能做错的事情感到困惑(假设这不是错误)。- 除了放弃整个
>> FluidLayer('prop1',1) Error using FluidLayer Invalid argument list. ...
方法(我想保留参数名称建议)以外,可以采取什么措施来解决这个问题?我考虑过过渡到arguments
和/或使用arguments
-但是这些需要大量的工作。varargin
functionSignatures.json
approach
functionSignatures.json
现在classdef Layer < handle & matlab.mixin.Heterogeneous properties (GetAccess = public, SetAccess = protected) prop1(1,1) double {mustBeNonempty} = NaN prop2(1,1) double {mustBeNonempty} = NaN end % properties methods (Access = protected) function layerObj = Layer(props) % A protected/private constructor means this class cannot be instantiated % externally, but only through a subclass. arguments props.?Layer end % Copy field contents into object properties fn = fieldnames(props); for idxF = 1:numel(fn) layerObj.(fn{idxF}) = props.(fn{idxF}); end end % constructor end % methods end
引发您所描述的错误。因此,它与子类化或继承无关。
但是,如果我们取消了
classdef FluidLayer < Layer properties (GetAccess = public, SetAccess = protected) % Subclass-specific properties end % properties methods function layerObj = FluidLayer(props) arguments props.?FluidLayer end % Create superclass: propsKV = namedargs2cell(props); layerObj = layerObj@Layer(propsKV{:}); % Custom modifications: end % constructor end % methods end
限制(将属性保留为public get和set访问权限,那么所有这些都将按您的预期工作。我不知道为什么限制设置访问权限会限制此用例,因为它与编写这些属性无关,并且类方法无论如何都应该具有设置访问权限。我的猜测是这是一个错误。