加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi中的实例引用?

发布时间:2020-12-15 04:31:47 所属栏目:大数据 来源:网络整理
导读:在C中Delphi相当于’this’是什么?你能举一些使用它的例子吗? 解决方法 在大多数情况下,您不应该在方法中使用self. 事实上,就像有一个隐含的自我.在类方法中访问类属性和方法时的前缀: type TMyClass = class public Value: string; procedure MyMethod;
在C中Delphi相当于’this’是什么?你能举一些使用它的例子吗?

解决方法

在大多数情况下,您不应该在方法中使用self.

事实上,就像有一个隐含的自我.在类方法中访问类属性和方法时的前缀:

type
  TMyClass = class
  public
    Value: string;
    procedure MyMethod;
    procedure AddToList(List: TStrings);
  end;


procedure TMyClass.MyMethod;
begin
  Value := 'abc';
  assert(self.Value='abc'); // same as assert(Value=10)
end;

当您想要将当前对象指定给另一个方法或对象时,将使用self.

例如:

procedure TMyClass.AddToList(List: TStrings);
var i: integer;
begin
  List.AddObject(Value,self);
  // check that the List[] only was populated via this method and this object
  for i := 0 to List.Count-1 do 
  begin
    assert(List[i]=Value);
    assert(List.Objects[i]=self);
  end;
end;

上面的代码将向TStrings列表添加一个项目,List.Objects []指向TMyClass实例.它将检查列表中所有项目的情况.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读