面向对象

类的定义

classdef MyClass < SuperClass1 & SuperClass2
   properties
      Prop1
      Prop2
   end
   methods
      function obj = MyClass(input1, input2)
         obj.Prop1 = input1;
         obj.Prop2 = input2;
      end
      function output = myMethod(obj, input)
         output = obj.Prop1 + obj.Prop2 + input;
      end
   end
end

值类型和引用类型

Untitled

属性控制

Untitled

构造函数

classdef MyClass
   properties
      Prop1
      Prop2
   end
   methods
      function obj = MyClass(input1, input2)
         obj.Prop1 = input1;
         obj.Prop2 = input2;
      end
   end
end

定时器Timer Class

function myTimer()
   t = timer('ExecutionMode', 'fixedRate', 'Period', 1, 'TimerFcn', @myCallback);
   start(t);
end

function myCallback(src, event)
   disp('Timer fired!');
end

其他参数

#如果计时器是一个类成员

classdef lightController < handle
    properties
        state=0;
        lightUI
        t
        greenTime=5;
        yellowTime=1;
        redTime=2;
        greenCount=0;
        yellowCount=0;
        redCount=0;
    end
    
    methods
        function obj=lightController()
            obj.lightUI=lightUI;
            obj.lightUI.controller=obj;
            obj.t=timer;
            [email protected];
            obj.t.Period=1;
            obj.t.ExecutionMode='fixedRate';
        end
        
        function countOneSec(Obj,~,~)
            switch Obj.state
                case 0
                case 1
                    if Obj.greenCount>=Obj.greenTime
                        Obj.state=2;
                        Obj.greenCount=0;
                    else
                        Obj.greenCount=Obj.greenCount+1;
                    end
                case 2
                    if Obj.yellowCount>=Obj.yellowTime
                        Obj.state=3;
                        Obj.yellowCount=0;
                    else
                        Obj.yellowCount=Obj.yellowCount+1;
                    end
                case 3
                    if Obj.redCount>=Obj.redTime
                        Obj.state=1;
                        Obj.redCount=0;
                    else
                        Obj.redCount=Obj.redCount+1;
                    end
                    
            end
            Obj.lightUI.changeColor(Obj.state);
        end
        
    end
    
    
    
end

App Designer