Matlab Handle 类和 Value 类以及类的特性

一、Handle 类

>> fig1a=figure(1)

fig1a = 

  Figure (1) - 属性:

      Number: 1
        Name: ''
       Color: [0.9400 0.9400 0.9400]
    Position: [573 544 560 420]
       Units: 'pixels'

  显示 所有属性

>> fig1b=fig1a

fig1b = 

  Figure (1) - 属性:

      Number: 1
        Name: ''
       Color: [0.9400 0.9400 0.9400]
    Position: [989 483.6667 560 420]
       Units: 'pixels'

  显示 所有属性

>> fig1b

fig1b = 

  Figure (1) - 属性:

      Number: 1
        Name: ''
       Color: [0.9400 0.9400 0.9400]
    Position: [989 483.6667 560 420]
       Units: 'pixels'

  显示 所有属性

>> fig1a.Color=[1,0,1]

fig1a = 

  Figure (1) - 属性:

      Number: 1
        Name: ''
       Color: [1 0 1]
    Position: [989 483.6667 560 420]
       Units: 'pixels'

  显示 所有属性

>> fig1b

fig1b = 

  Figure (1) - 属性:

      Number: 1
        Name: ''
       Color: [1 0 1]
    Position: [989 483.6667 560 420]
       Units: 'pixels'

  显示 所有属性

>> 

 没有继承Handle class 的就是value 类,继承handle 类的就是Handle 类。


>> isa(fig1a,'handle')

ans =

  logical

   1

 

 MyClassRectangleHandle 继承自handle 
 

classdef MyClassRectangleHandle < handle
    properties 
        len=0;
        width=0;
    end
    
    
    methods
        function  obj=MyClassRectangleHandle(l,w)
            if nargin==2
                obj.len=l;
                obj.width=w;
            end
        end
        
        
        function  outarg=rectArea(obj)
            outarg= obj.len * obj.width;
        end
        
        
        % 重写这个函数
        function  out= Itxoap(obj1,obj2)
            out = rectArea(obj1)<rectArea(obj2);
        end
        function  out=gts(obj1,obj2)
            if isa(obj2,'MyClassRectangleHandle')
            out =gt(rectArea(obj1),rectArea(obj2));
            else
                inp2=MyClassRectangleHandle(obj2,1);
                 out =gt(rectArea(obj1),rectArea(inp2));
            end
        end
            
        function  disp(obj)
            fprintf("length :%.2f  ,   width: %.2f    \n ",obj.len,obj.width);
        end
        
    end

end

test:

>> clear all
>> rect1=MyClassRectangleHandle(1,2)

rect1 = 

length :1.00  ,   width: 2.00    
 >> rect3=MyClassRectangleHandle(2,3)

rect3 = 

length :2.00  ,   width: 3.00    
 >> rect2=rect1

rect2 = 

length :1.00  ,   width: 2.00    
 >> rect1==rect2

ans =

  logical

   1

>> rect1==rect3

ans =

  logical

   0

>> rect1.width=120

rect1 = 

length :1.00  ,   width: 120.00    
 >> rect2

rect2 = 

length :1.00  ,   width: 120.00    
 >> 

三、类的特性 -properties 

 父类:

classdef  MyClassAttribute <handle
    
    properties
        num=0;
    end
     properties (Access=protected)
        word='hello';
    end
    
    
    methods
        function obj=MyClassAttribute(num,word)
            if nargin==2
                obj.num=num;
                obj.word=word;  
            end 
        end
        
        function  showword(obj)
            disp(obj.word);
        end
        
    end
end

子类: 

classdef  MyClassSubClass < MyClassAttribute
    
    methods
        function changeNum(obj,num)
           obj.num=num;
              
        end
        
        function changeWoeds(obj,word)
          obj.word=word;  
        end
    end
end

四、类的特性 -Methods

 

classdef  valSet
    
    properties
        grade=0;
    end
    
    methods
        function obj=valSet(in)
            if nargin==1
                obj.grade=in;
            end
        end
        
        function obj=set.grade(obj,val)
            if val>=0 && val<100
                obj.grade=val;
            else
                error('grade is wrong !')
            end
        end
        
%         
%         function  get.grade()
%             
%         end


        % overload  pluss 
        function obj=plus(obj1,obj2)
            obj=valSet(obj1.grade+obj2.grade);  
        end
        
    end
    
    % 抽象方法
    methods(Abstract=true)
         obj=curve(obj);
    end
   
    
    % 
   methods(Hidden=true)
       function  showgrade(obj)
           disp(obj.grade);
       end
   end
    
   %  被封装的,在子类中不能重复定义
      methods(Sealed=true)
       function  showgrade2(obj)
           disp(obj.grade);
       end
      end
    
       methods(Static=true)
          function  showgrade_static()
           fprintf('showgrade_static  \n')
       end
       end
      
      
       
       % event 
       % listen
       % sone object event - listener   object  call back function 
   
    
end

 

五、事件

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值