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

德尔福 – Self的范围是什么?

发布时间:2020-12-15 09:27:26 所属栏目:大数据 来源:网络整理
导读:我要求提高我对自我的理解. 请考虑以下事项: type PTestObject = ^TTestObject; TTestObject = class(TObject) private FCaption : String; public procedure MakeThePointer; property Caption : String read FCaption write FCaption; end; TForm4 = clas
我要求提高我对自我的理解.

请考虑以下事项:

type
  PTestObject = ^TTestObject;
  TTestObject = class(TObject)
  private
    FCaption : String;
  public
    procedure MakeThePointer;

    property Caption : String read FCaption write FCaption;
  end;

  TForm4 = class(TForm)
    ButtonFirst: TButton;
    ButtonSecond: TButton;
    ButtonThird: TButton;
    procedure ButtonFirstClick(Sender: TObject);
    procedure ButtonSecondClick(Sender: TObject);
    procedure ButtonThirdClick(Sender: TObject);
  private
  public
  end;

var
  Form4: TForm4;
  PointerOfTest : PTestObject;
  TestObj : TTestObject;

implementation

{$R *.dfm}

procedure TTestObject.MakeThePointer;
begin
  PointerOfTest := @Self;
end;

procedure TForm4.ButtonFirstClick(Sender: TObject);
begin
  TestObj := TTestObject.Create;
  TestObj.Caption := 'Hello';
  TestObj.MakeThePointer;
end;

procedure TForm4.ButtonSecondClick(Sender: TObject);
begin
  TestObj.MakeThePointer;
  ShowMessage(PointerOfTest^.Caption);
end;

procedure TForm4.ButtonThirdClick(Sender: TObject);
begin
  // TestObj.MakeThePointer; - Because I do not do this I get Access Violation
  ShowMessage(PointerOfTest^.Caption);
end;

我们的想法是创建一个指向TestObj的Self的指针,然后再次访问它.如果我在相同的Click事件(ButtonSecondClick)中调用MakeThePointer,我在那里访问该指针它工作正常.如果我在访问指针之前没有调用MakeThePointer(ButtonThirdClick),那么似乎TestObj的Self不存在,以至于先前创建的指针有效并且我得到了访问冲突.

如果我错了,请纠正我,但我认为Self是每个对象方法的局部变量.因此,它将仅分别针对每种方法的范围?

现在考虑一下……如果是这种情况,那么为什么如果单击ButtonFirst,则下面的工作会起作用,然后是ButtonSecond?似乎Self变量已落在同一地址上,因此允许以下工作.我可以假设Self变量将始终位于同一地址或将更改吗?

type
  TFormOther = class(TForm)
    ButtonFirst: TButton;
    ButtonSecond: TButton;
    procedure ButtonFirstClick(Sender: TObject);
    procedure ButtonSecondClick(Sender: TObject);
  private
  public
    procedure MakeThePointer;
    procedure SetTheCaption;
  end;

var
  FormOther: TFormOther;
  PointerOfForm : ^TForm;

implementation

{$R *.dfm}

procedure TFormOther.MakeThePointer;
begin
  PointerOfForm := @Self;
end;

procedure TFormOther.SetTheCaption;
begin
  PointerOfForm^.Caption := 'Hello';
end;

procedure TFormOther.ButtonFirstClick(Sender: TObject);
begin
  MakeThePointer;
end;

procedure TFormOther.ButtonSecondClick(Sender: TObject);
begin
  SetTheCaption;
end;

解决方法

What is the scope of Self?

在一个方法中,Self最好被认为是一个局部变量.因此,它的地址@Self在方法返回之前是有效的.

这解释了您的代码失败的原因.您的代码在方法返回后取消引用指针,此时指针无效.

May I assume that the Self variable will always be on the same address?

不行,你不可以.

我认为你的问题从这里开始:

type
  PTestObject = ^TTestObject;

因为TTestObject是一个类,所以TTestObject类型的变量(例如Self)是一个引用.引用是指针的奇特名称.在这种情况下,您的Self,TTestObject的内部方法是指向实例的指针.

因此,使用TTestObject而不是^ TTestObject,您的问题就会消失.

(编辑:李大同)

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

    推荐文章
      热点阅读