面向对象的 MATLAB:来自父级的静态方法无法设置来自子级的受保护 属性
object oriented MATLAB: static method from parent can't set protected property from child
好的,所以我有一个父级 class MOM
,其中包含一组 SetAccess 属性设置为 protected
的属性。它有一个特殊的静态方法 setProperty
来设置它的属性:
classdef MOM < handle
properties (SetAccess = 'protected')
age;
occupation;
num_kids;
MOMmusthaves ={'age','occupation','num_kids'};
end
methods
function obj = MOM(varargin)
if iscell(varargin{1,1})
varargin =varargin{1,1};
end
inputstrings=varargin(cellfun(@isstring,varargin)); % get strings out of the inputs
if(isempty(setxor(intersect(inputstrings,obj.MOMmusthaves),obj.MOMmusthaves)))
% if all proper inputs are entered, initialize your object
for pp =1:length(obj.MOMmusthaves)
obj.setProperty(obj,varargin,obj.MOMmusthaves{pp});%find each input in the vararagins, and set it
end
else
display('Hey! These inputs are wrong. type help MOM for an example. Make sure you inlcude inputs for all of the following:'); obj.MOMmusthaves
return;
end
end
end
methods (Static)
function setProperty(obj,varargs,xx)
eval(sprintf('obj.%s = varargs{find(strcmp(''%s'',varargs))+1};',xx,xx));
end
end
end
然后我有一个子对象 KID
,它有几个属性,还有 SetAccess protected
。当我尝试使用 MOM
的静态方法在 KID
的构造函数中设置 KID
属性 时,出现错误:(
错误说:
You cannot set the read-only property 'allowance' of KID.
基本上,似乎 KID
认为它不能将 MOM
的静态方法用作自己的方法(因此没有正确继承它)。
我的问题是:
有什么方法可以使静态方法被回收并可用于 KID 自身的受保护属性?
仅供参考,这里有类似 KID
代码的内容;
classdef KID < MOM
properties (SetAccess = 'protected')
gender;
allowance;
favoritecandy;
KIDmusthaves ={'gender','allowance','favoritecandy'};
end
methods
function obj = KID(varargin)
obj = obj@MOM(varargin(:)); % Special construct for instantiating the superclass
inputstrings=varargin(cellfun(@isstring,varargin)); % get strings out of the inputs.
if(isempty(setxor(intersect(inputstrings,obj.KIDmusthaves),obj.KIDmusthaves)))
% if all proper inputs are entered, initialize your object
for pp =1:length(obj.KIDmusthaves)
%find each input in the vararagins, and set it
obj.setProperty(obj,varargin,obj.KIDmusthaves{pp});
end
else
display('please use correct input format. Make sure you include inputs for all of the following:');
obj.KIDmusthaves
return;
end
end
end
end
我不太确定错误的确切来源是什么;虽然,我认为这可能是由于 eval
.
隐藏了句柄对象的突变
无论如何,如果我理解 setProperty
的预期用途,我认为使用点符号以非 static
形式编写函数可能是最简单的(类似于 dynamic field names 与结构):
methods
function [] = setProperty(obj,musthaves,varargin)
keys = varargin(1:2:end);
values = varargin(2:2:end);
for pp =1:length(keys)
key = keys{k};
if any(strcmp(musthaves,key))
obj.(key) = values{pp};
end
end
end
end
其中 musthaves
是 属性 字符串的任意元胞数组
您还可以让 musthaves
成为一个字符串,表示 obj
属性 持有 属性 列表:
methods
function [] = setProperty(obj,musthaves,varargin)
keys = varargin(1:2:end);
values = varargin(2:2:end);
for pp =1:length(keys)
key = keys{k};
if any(strcmp(obj.(musthaves),key))
obj.(key) = values{pp};
end
end
end
end
好的,所以我有一个父级 class MOM
,其中包含一组 SetAccess 属性设置为 protected
的属性。它有一个特殊的静态方法 setProperty
来设置它的属性:
classdef MOM < handle
properties (SetAccess = 'protected')
age;
occupation;
num_kids;
MOMmusthaves ={'age','occupation','num_kids'};
end
methods
function obj = MOM(varargin)
if iscell(varargin{1,1})
varargin =varargin{1,1};
end
inputstrings=varargin(cellfun(@isstring,varargin)); % get strings out of the inputs
if(isempty(setxor(intersect(inputstrings,obj.MOMmusthaves),obj.MOMmusthaves)))
% if all proper inputs are entered, initialize your object
for pp =1:length(obj.MOMmusthaves)
obj.setProperty(obj,varargin,obj.MOMmusthaves{pp});%find each input in the vararagins, and set it
end
else
display('Hey! These inputs are wrong. type help MOM for an example. Make sure you inlcude inputs for all of the following:'); obj.MOMmusthaves
return;
end
end
end
methods (Static)
function setProperty(obj,varargs,xx)
eval(sprintf('obj.%s = varargs{find(strcmp(''%s'',varargs))+1};',xx,xx));
end
end
end
然后我有一个子对象 KID
,它有几个属性,还有 SetAccess protected
。当我尝试使用 MOM
的静态方法在 KID
的构造函数中设置 KID
属性 时,出现错误:(
错误说:
You cannot set the read-only property 'allowance' of KID.
基本上,似乎 KID
认为它不能将 MOM
的静态方法用作自己的方法(因此没有正确继承它)。
我的问题是: 有什么方法可以使静态方法被回收并可用于 KID 自身的受保护属性?
仅供参考,这里有类似 KID
代码的内容;
classdef KID < MOM
properties (SetAccess = 'protected')
gender;
allowance;
favoritecandy;
KIDmusthaves ={'gender','allowance','favoritecandy'};
end
methods
function obj = KID(varargin)
obj = obj@MOM(varargin(:)); % Special construct for instantiating the superclass
inputstrings=varargin(cellfun(@isstring,varargin)); % get strings out of the inputs.
if(isempty(setxor(intersect(inputstrings,obj.KIDmusthaves),obj.KIDmusthaves)))
% if all proper inputs are entered, initialize your object
for pp =1:length(obj.KIDmusthaves)
%find each input in the vararagins, and set it
obj.setProperty(obj,varargin,obj.KIDmusthaves{pp});
end
else
display('please use correct input format. Make sure you include inputs for all of the following:');
obj.KIDmusthaves
return;
end
end
end
end
我不太确定错误的确切来源是什么;虽然,我认为这可能是由于 eval
.
无论如何,如果我理解 setProperty
的预期用途,我认为使用点符号以非 static
形式编写函数可能是最简单的(类似于 dynamic field names 与结构):
methods
function [] = setProperty(obj,musthaves,varargin)
keys = varargin(1:2:end);
values = varargin(2:2:end);
for pp =1:length(keys)
key = keys{k};
if any(strcmp(musthaves,key))
obj.(key) = values{pp};
end
end
end
end
其中 musthaves
是 属性 字符串的任意元胞数组
您还可以让 musthaves
成为一个字符串,表示 obj
属性 持有 属性 列表:
methods
function [] = setProperty(obj,musthaves,varargin)
keys = varargin(1:2:end);
values = varargin(2:2:end);
for pp =1:length(keys)
key = keys{k};
if any(strcmp(obj.(musthaves),key))
obj.(key) = values{pp};
end
end
end
end