Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用MATLAB中的静态函数计算创建的实例数? 在C++之后,我正在MATLAB学习OOP。我试图创建一个静态函数来获取为类创建的numOfInstances。此外,一个对象中的更改应反映其他对象中的更改。下面是我的代码: classdef (Sealed) Student < handle properties (GetAccess = 'public', SetAccess = 'public') Name; ID; end methods (Access = private) function obj = Student end end methods (Static) function singleObj = getInstances persistent localObj; if isempty(localObj) || ~isvalid(localObj) localObj = Student; end singleObj = localObj; end end methods (Static) function count = getNumInstances persistent objCount; if isempty(objCount) objCount = 1; else objCount = objCount + 1; end count = objCount; end end end classdef(密封)学生_Oop_Matlab_Singleton - Fatal编程技术网

如何使用MATLAB中的静态函数计算创建的实例数? 在C++之后,我正在MATLAB学习OOP。我试图创建一个静态函数来获取为类创建的numOfInstances。此外,一个对象中的更改应反映其他对象中的更改。下面是我的代码: classdef (Sealed) Student < handle properties (GetAccess = 'public', SetAccess = 'public') Name; ID; end methods (Access = private) function obj = Student end end methods (Static) function singleObj = getInstances persistent localObj; if isempty(localObj) || ~isvalid(localObj) localObj = Student; end singleObj = localObj; end end methods (Static) function count = getNumInstances persistent objCount; if isempty(objCount) objCount = 1; else objCount = objCount + 1; end count = objCount; end end end classdef(密封)学生

如何使用MATLAB中的静态函数计算创建的实例数? 在C++之后,我正在MATLAB学习OOP。我试图创建一个静态函数来获取为类创建的numOfInstances。此外,一个对象中的更改应反映其他对象中的更改。下面是我的代码: classdef (Sealed) Student < handle properties (GetAccess = 'public', SetAccess = 'public') Name; ID; end methods (Access = private) function obj = Student end end methods (Static) function singleObj = getInstances persistent localObj; if isempty(localObj) || ~isvalid(localObj) localObj = Student; end singleObj = localObj; end end methods (Static) function count = getNumInstances persistent objCount; if isempty(objCount) objCount = 1; else objCount = objCount + 1; end count = objCount; end end end classdef(密封)学生,oop,matlab,singleton,Oop,Matlab,Singleton,您需要增加构造函数中的实例数,而您当前没有这样做。下面是我大致的做法 classdef cldef < handle methods (Static, Access = private) function oldValue = getOrIncrementCount(increment) % Private function to manage the counter persistent VALUE

您需要增加构造函数中的实例数,而您当前没有这样做。下面是我大致的做法

classdef cldef < handle
    methods (Static, Access = private)
        function oldValue = getOrIncrementCount(increment)
        % Private function to manage the counter
            persistent VALUE
            if isempty(VALUE)
                VALUE = 0;
            end
            oldValue = VALUE;
            if nargin > 0
                VALUE = VALUE + increment;
            end
        end
    end
    methods (Static)
        function value = getInstanceCount()
        % Public access to the counter cannot increment it
            value = cldef.getOrIncrementCount();
        end
    end
    methods
        function obj = cldef()
        % Increment the counter in the constructor
            cldef.getOrIncrementCount(1);
        end
    end
end
classdef cldef0
价值=价值+增量;
结束
结束
结束
方法(静态)
函数值=getInstanceCount()
%对计数器的公共访问不能增加它
value=cldef.getOrIncrementCount();
结束
结束
方法
函数obj=cldef()
%递增构造函数中的计数器
cldef.getOrIncrementCount(1);
结束
结束
结束