是否可以有两个同名的属性?
Is it possible to have two properties with the same name?
是否可以有两个属性同名?
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];
编译fine.Don不知道这是否得到官方支持或者是否有其他问题,但是它编译正常并调用正确的getter/setter(在 Delphi 2010 年测试)。
这当然只有在您还没有为您的 class 使用默认值 属性 时才有效,因为我能够使它起作用的唯一方法是通过默认子句。
是否可以有两个属性同名?
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];
编译fine.Don不知道这是否得到官方支持或者是否有其他问题,但是它编译正常并调用正确的getter/setter(在 Delphi 2010 年测试)。
这当然只有在您还没有为您的 class 使用默认值 属性 时才有效,因为我能够使它起作用的唯一方法是通过默认子句。