我正在编写一个乘以它的两个参数的乘法器类。如果未给出参数,则给出默认值 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