因为对象中数据的共享性,即对象调用类方法时,是不需要传递参数的,而函数则不同,每一次调用函数都需要进行参数的传递,因此有时通过创建对象调用类方法可以提高代码效率。见下面例子:
Function classOptimize();
Begin
a := rand(10000,2);
b := rand(10000,2);
c := rand(10000,2);
myobj := CreateObject("MyClass",a,b,c);
mtic;
for i := 0 to 100 do
Ssum(a,b,c);
t1 := mtoc;
mtic;
for i := 0 to 100 do
myobj.classSum;
t2 := mtoc;
return array(t1,t2);
End;
function Ssum(a,b,c); //函数求和
begin
return a+b+c;
end
Type MyClass = Class //类
private
a,b,c;
public
function Create(a1,b1,c1); //初始化方法
begin
a := a1;
b := b1;
c := c1;
end
function classSum(); //求和运算
begin
return a+b+c;
end
End;
运行结果:array(3.0027,2.9396),说明创建对象再调用类方法还是可以提高代码效率的。