matlab面向对象编程基本

1 基本框架,函数声明

classdef 声明类,’.m’文件名和类名一致。

hande 是父类;< 表示继承

(Static = true)声明静态methods,在CommondLine可使用 tools.test(1,2)调用。


classdef tools < handle  
    methods (Static = true)  
        function a = test(b, c) 
            a = b + c;  
        end  
    end  
end




2 基本框架,变量声明

classdef ClassName
    properties
        a=0;
        b=1;
        c=2;
        d=[1 2 3; 4 5 6];
        e={'xx', 1; 'yy', 2};
        re;
end

    properties(SetAccess = private) % 外部只有只读权限
        h = [1 2 3];
end

properties(GetAccess = private)
        h_private;
end

    properties(Constant) 
        h_Static = 3.1415926;
    end

end



3 构造函数

返回值 类型的变量

varargin 输入参数向量

varargout 输出参数向量

nargin 输入参数数量

nargout 输出参数数量

classdef className
    properties
        a=0;
        b=1;
        c=2;
        d=[1 2 3; 4 5 6];
        e={'xx', 1; 'yy', 2};
        re;
    end
    
    methods
        function classNameTypeVar = className(x1, x2, varargin)
            if nargin == 0 % nargin 内置变量,输入参数的数量
            else
                % a,b are members of class : className
                % class construction function return a class
                classNameTypeVar.a = x1;
                classNameTypeVar.b = x2;
            end
            
            if nargout > 1 % nargout 内置变量,输出参数的数量
                error('有木有搞错啊! ');
            end    
        end
    end
    
end


4 Dependet 依赖型变量 Balance

Balance的值取决于CurrencyDollarAmount,调用时计算(calculatedonly when asked for)。

obj表示自己

get.就是语法

format bank保留小数点后两位有效数字

classdef Account
   properties
      Currency
      DollarAmount
   end
   properties (Dependent)
      Balance
   end
   ...
   methods
      function value = get.Balance(obj)
         c = obj.Currency;
         switch c
            case 'E'
               v = obj.DollarAmount / 1.1;
            case 'P'
               v = obj.DollarAmount / 1.5;
            otherwise
               v = obj.DollarAmount;
         end
         format bank
         value = v;
      end
   end
end




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值