如何在delphi中将变量解释为函数/命令?
我试图将变量解释为命令。 所以如何在delphi中将变量解释为函数/命令?,delphi,Delphi,我试图将变量解释为命令。 所以 Delphi没有脚本语言,所有内容都经过编译 例如,要将字符串解释为delphi指令,您需要一个脚本解析器。您可以为函数分配一个变量,以获得与您想要实现的类似的结果 有关程序类型的详细信息 我从Button1Click和Button2Click处理程序的内容中假设,您正在询问如何基于操作名称的文本操作调用算术操作。所以我有点困惑,你接受了一个答案,这个答案没有解决这个问题,但没关系 正如另一个答案告诉您的,Delphi是一个编译器,而不是脚本解释器本身,但是有许多
Delphi没有脚本语言,所有内容都经过编译
例如,要将字符串解释为delphi指令,您需要一个脚本解析器。您可以为函数分配一个变量,以获得与您想要实现的类似的结果 有关程序类型的详细信息
我从Button1Click和Button2Click处理程序的内容中假设,您正在询问如何基于操作名称的文本操作调用算术操作。所以我有点困惑,你接受了一个答案,这个答案没有解决这个问题,但没关系 正如另一个答案告诉您的,Delphi是一个编译器,而不是脚本解释器本身,但是有许多Delphi实现,而DWScript是一个特别优秀和强大的示例 这个答案的重点是基于操作名称的文本操作调用算术操作。当然,这只是通用脚本解释器(或者更简单的表达式求值器)功能的一小部分,但了解如何通过文本名称自己进行调用,而不必使用整个级联的“if OpName='xxx'then…else if…”语句,这是很有用的 在下面的代码中,TMethodWrapper类用于存储对特定整数操作的引用(为了简单起见,我忽略了Float/Double结果),并将该操作应用于调用的参数I1和I2。在其应用算子方法中进行了算法评估 然后使用TStringList“OpList”作为“字典”来存储包装器和操作名。Button1Click处理程序查找Edit1.Text中命名的操作,并将其应用于该操作的参数 我编写了这个简单的实现,以便它在D7中工作。显然,在最近的Delphi版本中,使用泛型工具,将涉及的原则应用于Integer以外的数据类型会容易得多,并且您可以使用RTTI来添加函数及其名称,而不是我在FormCreate中所做的。它们也有像TDictionary这样的对象,你可以在TStringList的节奏中使用
type
TIntegerOperation = function(Int1, Int2 : Integer) : Integer of object;
TMethodWrapper = Class
private
FOperation : TIntegerOperation;
public
constructor Create(AnOperation : TIntegerOperation);
function GetValue(I1, I2 : Integer) : Integer;
property Operation : TIntegerOperation read FOperation write FOperation;
end;
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
OpList : TStringList;
function Add(I1, I2 : Integer): Integer;
function Subtract(I1, I2 : Integer): Integer;
procedure ApplyOperator(OpName: String; I1, I2 : Integer);
public
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.Add(I1, I2 : Integer) : Integer;
begin
Result := I1 + I2;
end;
function TForm1.Subtract(I1, I2 : Integer) : Integer;
begin
Result := I1 - I2;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
OpList := TStringList.Create;
OpList.AddObject('Add', TMethodWrapper.Create(Add));
OpList.AddObject('Subtract', TMethodWrapper.Create(Subtract));
end;
constructor TMethodWrapper.Create(AnOperation: TIntegerOperation);
begin
inherited Create;
Operation := AnOperation;
end;
function TMethodWrapper.GetValue(I1, I2 : Integer): Integer;
var
IntegerOperation : TIntegerOperation;
begin
IntegerOperation := TIntegerOperation(Operation);
Result := IntegerOperation(I1, I2);
end;
procedure TForm1.ApplyOperator(OpName : String; I1, I2 : Integer);
var
Index : Integer;
Res : Integer;
MW : TMethodWrapper;
begin
Index := OpList.IndexOf(OpName);
if Index < 0 then Exit;
MW := TMethodWrapper(OpList.Objects[Index]);
Res := MW.GetValue(I1, I2);
Caption := IntToStr(Res);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ApplyOperator(Edit1.Text, 3, 5);
end;
类型
TINTEGropertion=function(Int1,Int2:Integer):对象的整数;
tMethodRapper=类
私有的
操作:染色分离;
公众的
构造函数创建(AnOperation:TinteRightation);
函数GetValue(I1,I2:整数):整数;
属性操作:TINTEGROPERATION read FOOPERATION write FOOPERATION;
结束;
TForm1=类(TForm)
编辑1:TEdit;
按钮1:t按钮;
程序按钮1点击(发送方:ToObject);
过程表单创建(发送方:ToObject);
私有的
操作列表:TStringList;
函数Add(I1,I2:Integer):整数;
函数减法(I1,I2:整数):整数;
过程ApplyOperator(OpName:String;I1、I2:Integer);
公众的
结束;
变量
表1:TForm1;
实施
{$R*.DFM}
函数TForm1.Add(I1,I2:Integer):Integer;
开始
结果:=I1+I2;
结束;
函数TForm1。减法(I1,I2:整数):整数;
开始
结果:=I1-I2;
结束;
过程TForm1.FormCreate(发送方:TObject);
开始
OpList:=TStringList.Create;
OpList.AddObject('Add',TMethodWrapper.Create(Add));
OpList.AddObject('Subtract',tmethodrapper.Create(Subtract));
结束;
构造函数tMethodRapper.Create(操作:TinteRightation);
开始
继承创造;
操作:=一次操作;
结束;
函数tmethodRapper.GetValue(I1,I2:Integer):整数;
变量
整数运算:着色聚合;
开始
IntegerOperation:=TinteGroupation(操作);
结果:=整数运算(I1,I2);
结束;
过程TForm1.ApplyOperator(OpName:String;I1,I2:Integer);
变量
索引:整数;
Res:整数;
MW:tMethodRapper;
开始
索引:=OpList.IndexOf(OpName);
如果指数<0,则退出;
MW:=tMethodRapper(OpList.Objects[Index]);
Res:=MW.GetValue(I1,I2);
标题:=IntToStr(Res);
结束;
程序TForm1.按钮1单击(发送方:TObject);
开始
ApplyOperator(Edit1.Text,3,5);
结束;
您希望我们如何知道您在说什么?您的意思可能是什么?我看到了一个公认的答案,您可能也对表达式解析器感兴趣,例如
type
TForm1 = class(TForm)
. . .
private
FOpp: function (AOp1, AOp2: Integer): Double;
. . .
end;
implementation
function OpAddFunction(AOp1, AOp2: Integer): Double;
begin
Result := AOp1 + AOp2;
end;
function OpSubFunction(AOp1, AOp2: Integer): Double;
begin
Result := AOp1 - AOp2;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FOpp := OpAddFunction;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FOpp := OpSubFunction;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
op1, op2: Integer;
begin
if not Assigned(FOpp) then
Exit;
op1 := 1;
op2 := 2;
Edit1.Text := FloatToStr(FOpp(op1, op2));
end;
end.
type
TIntegerOperation = function(Int1, Int2 : Integer) : Integer of object;
TMethodWrapper = Class
private
FOperation : TIntegerOperation;
public
constructor Create(AnOperation : TIntegerOperation);
function GetValue(I1, I2 : Integer) : Integer;
property Operation : TIntegerOperation read FOperation write FOperation;
end;
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
OpList : TStringList;
function Add(I1, I2 : Integer): Integer;
function Subtract(I1, I2 : Integer): Integer;
procedure ApplyOperator(OpName: String; I1, I2 : Integer);
public
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.Add(I1, I2 : Integer) : Integer;
begin
Result := I1 + I2;
end;
function TForm1.Subtract(I1, I2 : Integer) : Integer;
begin
Result := I1 - I2;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
OpList := TStringList.Create;
OpList.AddObject('Add', TMethodWrapper.Create(Add));
OpList.AddObject('Subtract', TMethodWrapper.Create(Subtract));
end;
constructor TMethodWrapper.Create(AnOperation: TIntegerOperation);
begin
inherited Create;
Operation := AnOperation;
end;
function TMethodWrapper.GetValue(I1, I2 : Integer): Integer;
var
IntegerOperation : TIntegerOperation;
begin
IntegerOperation := TIntegerOperation(Operation);
Result := IntegerOperation(I1, I2);
end;
procedure TForm1.ApplyOperator(OpName : String; I1, I2 : Integer);
var
Index : Integer;
Res : Integer;
MW : TMethodWrapper;
begin
Index := OpList.IndexOf(OpName);
if Index < 0 then Exit;
MW := TMethodWrapper(OpList.Objects[Index]);
Res := MW.GetValue(I1, I2);
Caption := IntToStr(Res);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ApplyOperator(Edit1.Text, 3, 5);
end;