Although I'm still not entirely clear about the meaning of the following code segment (I feel like it's misleading), I have successfully modified lines 35 and 36 of the Example Code to achieve overloading of {}.
%Brace indexing, operation:A{n}
switch S.type
case '{}'
B = A.CellProperty{S.subs{:}}; %What is CellProperty????
end
The modified Example Code
classdef MPolynom
properties
coef; %多项式系数
end
methods
function obj = MPolynom(c) %构造函数
if isa(c,'MPolynom')
obj.coef = c.coef;
else
obj.coef = c(:)';
end
end
function plot(obj) %重载plot函数
r = max(abs(roots(obj.coef)));
x = (-1.1:0.01:1.1)*r;
y = polyval(obj.coef,x);
plot(x,y);
xlabel('X')
ylabel('Y','Rotation',0)
grid on
end
function r = plus(obj1,obj2)
if ~isa(obj1,'MPolynom') %如果第一个参数不是类MPolynom对象
obj1 = MPolynom(obj1); %创建一个类MPolynom对象obj1
end
if ~isa(obj2,'MPolynom') %如果第二个参数不是类MPolynom对象
obj2 = MPolynom(obj2); %创建一个类MPolynom对象obj2
end
k = length(obj2.coef) - length(obj1.coef); %计算两个阵列的长度差
%创建一个类MPolynom对象作为返回值
r = MPolynom([zeros(1,k) obj1.coef]+[zeros(1,-k) obj2.coef]);
end
function b = subsref(a,s) %下标索引引用的实现
switch s(1).type
case '{}' %圆括号类型的引用
ind = s.subs{:};
b = polyval(a.coef,ind); %返回多项式的值
case '.' %“.”类型的引用
switch s(1).subs
case 'coef'
b = a.coef;
case 'plot' %由于方法plot没有返回值,这里单独列出访问过程
a.plot;
otherwise %其他带返回值方法的引用
if length(s)>1
b = a.(s(1).subs)(s(2).subs{:}); %带输入参数的方法引用
else
b = a.(s.subs); %不带输入参数的方法引用
end
end
otherwise
error('Specify value for x as obj(x)')
end
end
function y = polyval(obj,x) %计算多项式对象obj在x处的值
y = polyval(obj.coef,x);
end
end
end
p=MPolynom([1,3,2,5]);
p{2}
ans =
29