1

我正在 MATLAB 中使用 OOP(面向对象编程)进行简单测试。我做了一个简单的类,包括一个构造函数。另外,我做了三个关于属性的部分:三个常规属性;一个常数;和三个从属属性。我包含了一些常规属性的获取/设置方法、一个输出方法和一个构造函数使用的受保护/静态方法:'getPropertyValue(propertyName, inputCell)。

静态方法会导致问题。我正在发送两个参数:字符串类型的“propertyName”和单元格类型的“inputCell”。最后一个参数应该发送从构造函数中定义的 varargin 复制的所有输入参数。

当我尝试使用该类并在命令行中创建它的实例时,

a = FigureObjects('promien',10)

我收到此错误:

'cell' 类型的输入参数的未定义函数 'getPropertyValue'。

FigureObjects 中的错误(第 26 行)[temp,status,msgStatus] = getPropertyValue('podstawa',inputCell);

我虽然可以将任何内容发送到任何函数,例如字符串、单元格、矩阵等。有没有办法将单元格发送到该函数并读取其内容?

谢谢

PS。这是我的代码:

classdef FigureObjects
%FIGUREOBJECTS Summary of this class goes here
%   Detailed explanation goes here

properties
    podstawa
    wysokosc
    promien
end

properties (Constant = true, Hidden = true)
    liczbaPi = 3.14
end

properties (Dependent = true)
    obwodKola
    obwodProstokata
    obwodKwadratu
end

methods
    % Constructor
    function obj = FigureObjects(varargin)
        inputCell = varargin;
        if nargin > 0
            [temp,status,msgStatus] = getPropertyValue('podstawa',inputCell);
            if status
                obj.podstawa = temp;
            else
                error(msgStatus);
            end
            [temp,status,msgStatus] = getPropertyValue('wysokosc',inputCell);
            if status
                obj.wysokosc = temp;
            else
                error(msgStatus);
            end
            [temp,status,msgStatus] = getPropertyValue('promien',inputCell);
            if status
                obj.promien = temp;
            else
                error(msgStatus);
            end
        end
    end

    % get functions
    function obwodKola = get.obwodKola(obj)
        if ~isempty(obj.promien)
            obwodKola = 2*FigureObjects.liczbaPi*obj.promien;
        else
            error('''promien'' is not defined.');
        end
    end
    function obwodProstokata = get.obwodProstokata(obj)
        if (~isempty(obj.podstawa) && ~isempty(obj.wysokosc))
            obwodProstokata = 2*(obj.podstawa*obj + obj.wysokosc);
        else
            error('Either ''podstawa'' or/and ''wysokosc'' are not defined.');
        end
    end
    function obwodKwadratu = get.obwodKwadratu(obj)
        if ~isempty(obj.podstawa)
            obwodKwadratu = 4*obj.podstawa;
        else
            error('''podstawa'' is not defined.');
        end
    end
    function promien = get.promien(obj)
        promien = obj.promien;
    end
    function podstawa = get.podstawa(obj)
        podstawa = obj.podstawa;
    end
    function wysokosc = get.wysokosc(obj)
        wysokosc = obj.wysokosc;
    end

    % set functions
    function obj = set.promien(obj,promien)
        if isnumeric(promien)
            obj.promien = promien;
        else
            error('''promien'' must be numeric.');
        end
    end
    function obj = set.podstawa(obj,podstawa)
        if isnumeric(promien)
            obj.podstawa = podstawa;
        else
            error('''podstawa'' must be numeric.');
        end
    end
    function obj = set.wysokosc(obj,wysokosc)
        if isnumeric(wysokosc)
            obj.wysokosc = wysokosc;
        else
            error('''wysokosc'' must be numeric.');
        end
    end

    % output functions
    function output = obwod(theFigureObj, rodzajFigury)
        if strcmpi(rodzajFigury,'kolo')
            output = theFigureObj.obwodKola;
        elseif strcmpi(rodzajFigury,'prostokat')
            output = theFigureObj.obwodProstokata;
        elseif strcmpi(rodzajFigury,'kwadrat')
            output = theFigureObj.obwodKwadratu;
        else
            error('Cannot identify a figure.');
        end
    end % obwod
end % methods

methods (Access = protected, Static)
    function [propertyValue, status, msgString] = ...
            getPropertyValue(propertyName, inputCell)
        I = strcmpi(propertyName,inputCell);
        if sum(I) ~= 0
            if (find(I)+1) <= length(inputCell)
                propertyValue = inputCell{find(I)+1};
                status = true;
                msgString = 'No errors';
            else
                propertyValue = [];
                status = false;
                msgString = ['Too few arguments for ''' propertyName '''.'];
            end
        else
            propertyValue = [];
            status = false;
            msgString = ['No property ' propertyName];
        end
    end % getPropertyValue(propertyName, cellArray)
end % methods (Access = protected, Static)

结束 % 类定义

4

1 回答 1

7

Just because the method is static, doesn't mean you can use it as if it were a separate function in the search path. You need to call obj.getPropertyValue or FigureObjects.getPropertyValue instead.

于 2013-04-15T14:29:58.157 回答