官方文档:[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内,比如
定义了类的两个成员变量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]
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]