Delphi – 动态调用不同的功能
发布时间:2020-12-15 04:24:34 所属栏目:大数据 来源:网络整理
导读:我有一个树状视图(VirtualTree),它有节点.当用户点击节点时,我需要运行一个特定的函数,传递节点的文本名.该功能是节点的属性之一.例如,假设两个节点. 节点1,Name = MyHouse,Function = BuildHouse 节点2,Name = MyCar,function = RunCar 当我点击节点1时,我
我有一个树状视图(VirtualTree),它有节点.当用户点击节点时,我需要运行一个特定的函数,传递节点的文本名.该功能是节点的属性之一.例如,假设两个节点.
节点1,Name = MyHouse,Function = BuildHouse 当我点击节点1时,我需要调用BuildHouse(‘MyHouse’)函数; 参数总是字符串.应该注意,这些是真正的功能,而不是类的成员. 有太多的节点要有一个CASE或IF / THEN类型的代码结构.我需要一种动态调用各种功能的方法,即无需对行为进行硬编码. 谢谢, 解决方法
Delphi允许创建指向函数的变量,然后通过变量调用函数.因此,您可以创建函数并将函数分配给节点的正确类型属性(或者可以将功能分配给例如许多集合项类的便利数据属性).
interface type TNodeFunction = function(AInput: String): String; implementation function Func1(AInput: String): String; begin result := AInput; end; function Func2(AInput: String): String; begin result := 'Fooled You'; end; function Func3(AInput: String): String; begin result := UpperCase(AInput); end; procedure Demonstration; var SomeFunc,SomeOtherFunc: TNodeFunction; begin SomeOtherFunc = Func3; SomeFunc := Func1; SomeFunc('Hello'); // returns 'Hello' SomeFunc := Func2; SomeFunc('Hello'); // returns 'Fooled You' SomeOtherFunc('lower case'); // returns 'LOWER CASE' end; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |