classdef Point2D < handle properties x y end properties(Constant) % 常量属性 R = pi/180; end properties(Dependent) % 非独立属性 r % 不占用存储空间,可以通过p1.r调用 end properties(Hidden) % 隐藏属性 var % 不会显示,开发、交互时有用 end methods functionresult = get.r(obj) result = sqrt(obj.x^2 + obj.y^2); % 通过get方法进行调用计算 end normalize(obj); % 仅提供声明,可在外部独立文件中编程 % 需要创建名为@Point2D的文件夹,把这些都丢进去 end end
% Point3D.m classdef Point3D < Point2D properties z end methods functionobj = Point3D(x0,y0,z0); obj = obj@Point2D(x0,y0); % 用@表示调用父类,子类必须先调用父类 obj.z = z0; end functionprint(obj); print@Point2D(obj); % 调用同名方法:函数名@父类类名(obj,其它参数) disp(['z=',num2str(obj.z)]); end end end
classdef Mouse < handle % ... end ... % Other Classes in other Matlab Files classdef Face < handle % 使用handle而非多重继承:Mouse&Eye&Nose properties mouse ... % Others Like eye/nose/ear end methods functionobj=Face() obj.mouse = Mouse(); ... % Others Like Eye/Nose/Ear end end end
classdef Point_Quadrant1 < handle properties x y end methods functionset.x(obj,valx) if (valx<0) error('Not in First Quadrant'); else obj.x = valx; end end functionset.y(obj,valy) % ...略 end end end