我正在编写一个Multiplier类,它将两个参数相乘。如果未给出参数,则给出默认值1。 现在当我更新一个变量时,我没有得到确切的产品。我的代码如下:
classdef Multiplier
properties (SetAccess = public, GetAccess = public)
first;
second;
end
properties(SetAccess = immutable, GetAccess = public)
product;
end
methods
function obj = Multiplier(varargin)
if nargin == 0
obj.first = 1;
obj.second = 1;
end
if nargin == 1
obj.first = varargin{1};
obj.second = 1;
end
if nargin > 1
obj.first = varargin{1};
obj.second = varargin{2};
end
obj.product = obj.first * obj.second;
end
end
end
k = Multiplier
k.first = 5
k.product -> This should return 5