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

WeG*_*ars 13 delphi delphi-xe7

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

property  Cell [Cl, Rw: Integer]: string   read getCell  write setCell;
property  Cell [ColName: string; Rw: Integer]: string read getCellByCol write setCellByCol;
Run Code Online (Sandbox Code Playgroud)

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

Hea*_*are 26

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

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;
Run Code Online (Sandbox Code Playgroud)

诀窍是将属性命名为相同,并使用"default"子句标记它们.然后,您可以使用各种参数访问相同的属性名称:

P1['k',1]:=P1[2,1];
P1[2,1]:=P1['k',1];
Run Code Online (Sandbox Code Playgroud)

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

这当然只有在你没有为你的类使用默认属性时才有效,因为我能够使它工作的唯一方法是通过default子句.

  • Upvoted,我学到了新东西!http://docwiki.embarcadero.com/RADStudio/Seattle/en/Properties#Array_Properties*类只能有一个具有给定签名的默认属性(数组参数列表),但可以重载默认属性.更改或隐藏后代类中的默认属性可能会导致意外行为,因为编译器始终静态绑定到属性.* (4认同)