请看 http://www.delphibbs.com/delphibbs/dispq.asp?lid=0583197 中我的回答。
过程/函数类型一般在要对数据进行事先不能确定的操作时使用的。举个例子吧:如果小
王来玩,我会给他倒杯可乐;如果小李来玩,我会给他倒杯茶。你可以将倒杯可乐和倒杯茶
做成两个过程,针对不同的客人。代码如下:
type
TDrinkProc=procedure(Guest:TGuest);
procedure Drink_Cola(Guest:TGuest);
begin
Guest.Drink:=1;
//cola
end;
procedure Drink_Tea(Guest:TGuest);
begin
Guest.Drink:=2;
//tea
end;
procedure Drink_Other(Guest:TGuest);
var
Drink:Integer;
begin
Drink:=Guest.AskFavoriteDrink;
//询问客人喜欢的饮料
Guest.Drink:=Drink;
end;
procedure GiveGuestDrink(Guest:TGuest);
var
DrinkProc:TDrinkProc;
begin
if Guest.Name='小王' then
DrinkProc:=Drink_Cola
else
if Guest.Name='小李' then
DrinkProc:=Drink_Tea
else
DrinkProc:=Drink_Other;
DrinkProc(Guest);
end;