class的结构: http://cn.mathworks.com/help/matlab/object-oriented-programming-in-matlab.html
class内的变量、方法需要放到相应的block内,比如
properties (SetAccess=protected) a=1 b=2 end
定义了类的两个成员变量a和b。 并且其默认值分别为1、2.
而成员变量还可以设置各种属性,比如(SetAccess=protected) 则使得该成员变量只能被该类及子类修改。
成员变量的属性大全: http://cn.mathworks.com/help/matlab/matlab_oop/property-attributes.html
class可以定义在一个.m文件中,也可以将类的函数分开,放到一个文件夹中。
放到文件夹中的方法为: http://cn.mathworks.com/help/matlab/matlab_oop/class-files-and-folders.html
需要 @ClassName 作为文件夹名称
Matlab中的class有两类,一类 类似平时的变量, 一类 类似指针,继承自handle类
觉着看完下面最简单的例子就不影响我们开始使用了~
最简单的例子: http://cn.mathworks.com/help/matlab/matlab_oop/getting-familiar-with-classes.html
注意matlab中定义成员方法必须显示包含该变量(必须为第一个参数)(名称不一定非要是obj)!!!
引用
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
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
指针类:
以下摘自: Comparison of MATLAB and Other OO Languages http://cn.mathworks.com/help/matlab/matlab_oop/matlab-vs-other-oo-languages.html
这篇文章很好,讲了 get set方法, 构造函数等
引用
classdef SimpleHandleClass < handle
properties
Color
end
methods
function obj = SimpleHandleClass(c)
if nargin > 0
obj.Color = c;
end
end
end
end
properties
Color
end
methods
function obj = SimpleHandleClass(c)
if nargin > 0
obj.Color = c;
end
end
end
end
Class Components http://cn.mathworks.com/help/matlab/matlab_oop/class-components.html
Representative Class Code http://cn.mathworks.com/help/matlab/matlab_oop/a-class-code-listing.html