1

これが私のコードです:

fm:

classdef f < handle
    properties (Access = public)
        functionString = '';
        x;
    end
    methods
        function obj = f
            if nargin == 0
                syms s;
                obj.x = input('Enter your function: ');
                obj.functionString = ilaplace(obj.x);
            end
        end
        function value = subsref(obj, a)
            t = a.subs{:};
            value = eval(obj.functionString);
        end
        function display(obj)
        end
    end
end

test.m:

syms s t;
[n d] = numden(f.x); % Here I want to use x, which is the user input, How can I do such thing?
zeros = solve(n);
poles = solve(d);
disp('The Poles:');
disp(poles);
disp('The Zeros:');
disp(zeros);
disp('The Result:');
disp(z(t));
disp('The Initial Value:');
disp(z(0));
disp('The Final Value:');
disp(z(Inf));

コマンドウィンドウでtestと入力すると、次のように表示されます。

>> test
??? The property 'x' in class 'f' must be accessed from a class instance because it
is not a Constant property.
4

1 に答える 1

3

Alex が指摘しているようfに、 member プロパティにアクセスするには、次のxように のインスタンスが必要です。

myf = f();
f.x

xパブリック プロパティとして定義されているため、取得するためのアクセサー メソッドは必要ありません。プライベートにすることを選択した場合はx、次のようなアクセサー メソッドが必要になります。

function x = getX( obj )
  x = obj.x;
end
于 2011-03-07T07:15:21.417 に答える