function TCount.Value : string;
begin
Result := ''; <--------------------//注意这里
i := 1;
while i <= 3 do
begin
j := i;
while j <= 5 do
begin
Result := Result + ' ' + IntToStr(j); <--------------------//注意这里
Inc(j);
end;
Inc(i);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ACount : TCount;
begin
ACount := TCount.Create;
Memo1.Lines.Text := edit1.Text + ' ' + ACount.Value;
ACount.Free;
end;
终于弄明白了,原来第一Result 是第三Result里的值(指->Result := 它的值 + ' ' + IntToStr)。
对吗??
var
s: string;
begin
s := ''; <--------------------//注意这里
i := 1;
while i <= 3 do
begin
j := i;
while j <= 5 do
begin
s := s + ' ' + IntToStr(j); <--------------------//注意这里
Inc(j);
end;
Inc(i);
end;
Result := s;
end;
function TCount.Value : string;
begin
Result := ''; <----如果这个Result 去除的话。。
i := 1;
while i <= 3 do
begin
j := i;
while j <= 5 do
begin
Result := Result + ' ' + IntToStr(j); <----------------
Inc(j);
end;
Inc(i);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ACount : TCount;
begin
ACount := TCount.Create;
Memo1.Lines.Text := edit1.Text + ' ' + ACount.Value;
ACount.Free;
end;
那这个 Result := Result + ' ' + IntToStr(j); 的 “:= Result +” 是什么值,而又从哪里来??
function FUN: string;
begin
Result := 'sssssss';
end;与function FUN: string;
begin
FUN := 'sssssss';
end;是等价的。
只不过在FUN作为被执行变量时,具有执行函数的功能,所以说是一种特殊的变量。
也就是说下面这里第二个FUN是具有执行函数功能的变量,也就是会出现死循环的原因。
function FUN: string;
begin
FUN := FUN+'sssssss';
end;
而采用result的作用就是将函数这个特殊的变量还原成普通变量,也就不具备执行函数的功能了。
所以下面这个代码就不会出现死循环了。
function FUN: string;
begin
RESULT := RESULT+'sssssss';
end;不过无论怎样,既然是变量,那就跟你自己声明的变量类似了。没有初始化就会直接成为该地址的原始内容,一般会跟这个变量后面的代码有关系,只不过一般都是乱码而已。所以一般都需要先初始化。