面向对象的MATLAB:来自父级的静态方法无法设置来自子级

时间:2015-05-16 01:19:54

标签: matlab oop inheritance properties static-methods

好的,所以我有一个父类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属性时,我收到错误:( 错误说:

  

您无法设置KID的只读属性'allowance'。

基本上,似乎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

1 个答案:

答案 0 :(得分:1)

我不确定错误的确切来源是什么;虽然,我认为这可能是由于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