一、Handle 类
>> fig1a=figure(1)
fig1a =
Figure (1) - 属性:
Number: 1
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [573 544 560 420]
Units: 'pixels'
显示 所有属性
>> fig1b=fig1a
fig1b =
Figure (1) - 属性:
Number: 1
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [989 483.6667 560 420]
Units: 'pixels'
显示 所有属性
>> fig1b
fig1b =
Figure (1) - 属性:
Number: 1
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [989 483.6667 560 420]
Units: 'pixels'
显示 所有属性
>> fig1a.Color=[1,0,1]
fig1a =
Figure (1) - 属性:
Number: 1
Name: ''
Color: [1 0 1]
Position: [989 483.6667 560 420]
Units: 'pixels'
显示 所有属性
>> fig1b
fig1b =
Figure (1) - 属性:
Number: 1
Name: ''
Color: [1 0 1]
Position: [989 483.6667 560 420]
Units: 'pixels'
显示 所有属性
>>
没有继承Handle class 的就是value 类,继承handle 类的就是Handle 类。
>> isa(fig1a,'handle')ans =
logical
1
MyClassRectangleHandle 继承自handle
classdef MyClassRectangleHandle < handle
properties
len=0;
width=0;
end
methods
function obj=MyClassRectangleHandle(l,w)
if nargin==2
obj.len=l;
obj.width=w;
end
end
function outarg=rectArea(obj)
outarg= obj.len * obj.width;
end
% 重写这个函数
function out= Itxoap(obj1,obj2)
out = rectArea(obj1)<rectArea(obj2);
end
function out=gts(obj1,obj2)
if isa(obj2,'MyClassRectangleHandle')
out =gt(rectArea(obj1),rectArea(obj2));
else
inp2=MyClassRectangleHandle(obj2,1);
out =gt(rectArea(obj1),rectArea(inp2));
end
end
function disp(obj)
fprintf("length :%.2f , width: %.2f \n ",obj.len,obj.width);
end
end
end
test:
>> clear all
>> rect1=MyClassRectangleHandle(1,2)
rect1 =
length :1.00 , width: 2.00
>> rect3=MyClassRectangleHandle(2,3)
rect3 =
length :2.00 , width: 3.00
>> rect2=rect1
rect2 =
length :1.00 , width: 2.00
>> rect1==rect2
ans =
logical
1
>> rect1==rect3
ans =
logical
0
>> rect1.width=120
rect1 =
length :1.00 , width: 120.00
>> rect2
rect2 =
length :1.00 , width: 120.00
>>
三、类的特性 -properties
父类:
classdef MyClassAttribute <handle
properties
num=0;
end
properties (Access=protected)
word='hello';
end
methods
function obj=MyClassAttribute(num,word)
if nargin==2
obj.num=num;
obj.word=word;
end
end
function showword(obj)
disp(obj.word);
end
end
end
子类:
classdef MyClassSubClass < MyClassAttribute
methods
function changeNum(obj,num)
obj.num=num;
end
function changeWoeds(obj,word)
obj.word=word;
end
end
end
四、类的特性 -Methods
classdef valSet
properties
grade=0;
end
methods
function obj=valSet(in)
if nargin==1
obj.grade=in;
end
end
function obj=set.grade(obj,val)
if val>=0 && val<100
obj.grade=val;
else
error('grade is wrong !')
end
end
%
% function get.grade()
%
% end
% overload pluss
function obj=plus(obj1,obj2)
obj=valSet(obj1.grade+obj2.grade);
end
end
% 抽象方法
methods(Abstract=true)
obj=curve(obj);
end
%
methods(Hidden=true)
function showgrade(obj)
disp(obj.grade);
end
end
% 被封装的,在子类中不能重复定义
methods(Sealed=true)
function showgrade2(obj)
disp(obj.grade);
end
end
methods(Static=true)
function showgrade_static()
fprintf('showgrade_static \n')
end
end
% event
% listen
% sone object event - listener object call back function
end
五、事件