gpt4 book ai didi

delphi - 是否可以有两个同名的属性?

转载 作者:行者123 更新时间:2023-12-03 14:36:53 28 4
gpt4 key购买 nike

是否可以有两个同名的属性?

property  Cell [Cl, Rw: Integer]: string   read getCell  write setCell;
property Cell [ColName: string; Rw: Integer]: string read getCellByCol write setCellByCol;

好吧,我尝试了,编译器不让我这样做,但也许有一个技巧......?

最佳答案

不 - 但话又说回来:是的......有点......

function    getP1(Cl,Rw : integer) : string;
procedure setP1(C1,Rw : integer ; const s : string);
function getP2(const Cl : string ; Rw : integer) : string;
procedure setP2(const C1 : string ; Rw : integer ; const s : string);
property P1[Cl,Rw : integer] : string read getP1 write setP1; default;
property P1[const Cl : string ; Rw : integer] : string read getP2 write setP2; default;

技巧是将属性命名为相同,并用“default”子句标记两者。然后您可以使用各种参数访问相同的属性名称:

P1['k',1]:=P1[2,1];
P1[2,1]:=P1['k',1];

编译良好。不知道这是否得到官方支持,或者是否存在其他问题,但是 它编译良好并调用正确的 getter/setter(在 Delphi 2010 中测试)。

当然,只有当您尚未为类使用默认属性时,这才有效,因为我能够使其工作的唯一方法是通过默认子句。

关于delphi - 是否可以有两个同名的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32584057/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com