Matlab中的面向对象编程

官方文档:[url]http://cn.mathworks.com/help/matlab/object-oriented-programming.html[/url]

class的结构:[url]http://cn.mathworks.com/help/matlab/object-oriented-programming-in-matlab.html[/url]

class内的变量、方法需要放到相应的block内,比如
   properties (SetAccess=protected)
a=1
b=2
end

定义了类的两个成员变量a和b。 并且其默认值分别为1、2.

而成员变量还可以设置各种属性,比如(SetAccess=protected) 则使得该成员变量只能被该类及子类修改。
成员变量的属性大全:[url]http://cn.mathworks.com/help/matlab/matlab_oop/property-attributes.html[/url]


class可以定义在一个.m文件中,也可以将类的函数分开,放到一个文件夹中。
放到文件夹中的方法为:[url]http://cn.mathworks.com/help/matlab/matlab_oop/class-files-and-folders.html[/url]
需要 @ClassName 作为文件夹名称


Matlab中的class有两类,一类[color=blue]类似平时的变量[/color], 一类[color=blue]类似指针[/color],继承自handle类
觉着看完下面最简单的例子就不影响我们开始使用了~

最简单的例子:[url]http://cn.mathworks.com/help/matlab/matlab_oop/getting-familiar-with-classes.html[/url]

注意matlab中定义成员方法必须显示包含该变量(必须为第一个参数)(名称不一定非要是obj)!!!
[quote]classdef BasicClass
properties
Value
end
methods
function r = roundOff(obj)
r = round([obj.Value],2);
end
function r = multiplyBy(obj,n)
r = [obj.Value] * n;
end
end
end[/quote]


指针类:
以下摘自:[color=blue]Comparison of MATLAB and Other OO Languages[/color][url]http://cn.mathworks.com/help/matlab/matlab_oop/matlab-vs-other-oo-languages.html[/url]
这篇文章很好,讲了 get set方法, 构造函数等

[quote]classdef SimpleHandleClass < handle
properties
Color
end
methods
function obj = SimpleHandleClass(c)
if nargin > 0
obj.Color = c;
end
end
end
end[/quote]

Class Components[url]http://cn.mathworks.com/help/matlab/matlab_oop/class-components.html[/url]


Representative Class Code[url]http://cn.mathworks.com/help/matlab/matlab_oop/a-class-code-listing.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值