Matlabで簡単なオブジェクト指向

数を数えるクラスの実装。

classdef testclass1 < handle
    
    % handle class (<-> value class). When the instance x1
    % is assigned to variable x2 (i.e. x2 = x1), reference to
    % x1 will be given instead of copy of x1.
    
    properties (SetAccess = private)
        counter
        
    end
    
    methods
        function CT = testclass1(initial_num)
           CT.counter = initial_num;
        end
        
        function count(CT, add)
           CT.counter = CT.counter + add;
        
        end
    
        function ret = get_counter(CT)
            ret = CT.counter;
   
        end
    end
end
 
%
% Running this code directly may cause error.
% After setting current directory to where this code
% is located, type
%
% ts1 = testclass1(100)
%
% Subsequently, you can
%
% ts1.count(20)
%
% Private properties cannot be changed externally
% (i.e. ts1.counter = 200)
%
% class(ts1)  ... Class of ts1
% fields(ts1) ... Properties of ts1?
% whos ts1    ... Associated information.
%
% Class name and file name probably must be the same.
%