Self变量,Self是一个内建变量,是在类方法实现区使用时,参考到该类方法对应的对象实体
 即:Self变量 是该类对应的对象的别名
 1Self的含义及举例unit Unit1;
 2Self的含义及举例
 3Self的含义及举例interface
 4Self的含义及举例
 5Self的含义及举例uses
 6Self的含义及举例  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7Self的含义及举例  Dialogs, StdCtrls;
 8Self的含义及举例type
 9Self的含义及举例  TPig = class(TObject)
10Self的含义及举例  public
11Self的含义及举例    pWeight: integer;
12Self的含义及举例    FUNCTION eat(WeightNow: integer;pFood: Integer): Integer;
13Self的含义及举例    { Public declarations }
14Self的含义及举例  end;
15Self的含义及举例type
16Self的含义及举例  TForm1 = class(TForm)
17Self的含义及举例    Button1: TButton;
18Self的含义及举例    Button2: TButton;
19Self的含义及举例    procedure Button1Click(Sender: TObject);
20Self的含义及举例    procedure Button2Click(Sender: TObject);
21Self的含义及举例  private
22Self的含义及举例    { Private declarations }
23Self的含义及举例  public
24Self的含义及举例    { Public declarations }
25Self的含义及举例  end;
26Self的含义及举例
27Self的含义及举例var
28Self的含义及举例  Form1: TForm1;
29Self的含义及举例
30Self的含义及举例implementation    {$R *.dfm}
31Self的含义及举例
32Self的含义及举例FUNCTION TPig.eat(WeightNow: integer;pFood: Integer): Integer;
33Self的含义及举例 VAR
34Self的含义及举例   wBefore, wNow: Integer;
35Self的含义及举例BEGIN
36Self的含义及举例 self.pWeight := WeightNow; //每次对象引用时,Self即代表对象名
37Self的含义及举例 //  pWeight := WeightNow; // 也是可以的
38Self的含义及举例  wBefore := self.pWeight;
39Self的含义及举例  self.pWeight := self.pWeight + (pFood div 6);
40Self的含义及举例  wNow := self.pWeight;
41Self的含义及举例  result := wNow - wBefore;
42Self的含义及举例  ShowMessage('原本重' + IntToStr(wBefore) + '公斤' + #13 +
43Self的含义及举例              '现在重' + IntToStr(wNow) + '公斤' + #13 +
44Self的含义及举例              '总共重了' + IntToStr(result) + '公斤'
45Self的含义及举例              );
46Self的含义及举例end;
47Self的含义及举例//在上面,声明TPig类时,还没有产生该类的对象实体,所以也无法预先知道引用该类的对象
48Self的含义及举例//名,但是在TPig.eat这个成员函数实现区,需要访问调用此方法的类对象的数据时,就
49Self的含义及举例//可以使用Self来表示该对象的名称,因此,不管TBig类所产生的对象的名称是什么,
50Self的含义及举例//都可用Self来访问对象的数据
51Self的含义及举例
52Self的含义及举例
53Self的含义及举例procedure TForm1.Button1Click(Sender: TObject);
54Self的含义及举例VAR
55Self的含义及举例  Pig1: TPig;
56Self的含义及举例  TDFood: Integer;
57Self的含义及举例begin
58Self的含义及举例  Pig1 := TPig.Create;
59Self的含义及举例  Pig1.eat(61,13) ;
60Self的含义及举例  ShowMessage('现在Pig1的重量是: ' + IntToStr(Pig1.pWeight) + '公斤');
61Self的含义及举例  TDFood := StrToInt(InputBox('今天要喂多少?','输入公斤数(整数)','11'));
62Self的含义及举例  Pig1.eat(Pig1.pWeight,TDFood);
63Self的含义及举例  Pig1.Free;
64Self的含义及举例end;
65Self的含义及举例
66Self的含义及举例procedure TForm1.Button2Click(Sender: TObject);
67Self的含义及举例VAR
68Self的含义及举例  Pig2: TPig;
69Self的含义及举例  TDFood: Integer;
70Self的含义及举例begin
71Self的含义及举例  Pig2 := TPig.Create;
72Self的含义及举例  Pig2.eat(61,13) ;
73Self的含义及举例  ShowMessage('现在Pig2的重量是: ' + IntToStr(Pig2.pWeight) + '公斤');
74Self的含义及举例  TDFood := StrToInt(InputBox('今天要喂多少?','输入公斤数(整数)','11'));
75Self的含义及举例  Pig2.eat(Pig2.pWeight,TDFood);
76Self的含义及举例  Pig2.Free;
77Self的含义及举例end;
78Self的含义及举例
79Self的含义及举例end
80Self的含义及举例
81Self的含义及举例

相关文章:

  • 2021-10-02
  • 2022-12-23
  • 2021-12-02
  • 2021-09-22
  • 2021-09-06
  • 2022-01-28
  • 2022-12-23
  • 2021-05-26
猜你喜欢
  • 2021-07-20
  • 2021-09-17
  • 2022-12-23
  • 2021-10-01
  • 2022-01-16
  • 2022-12-23
  • 2022-02-16
相关资源
相似解决方案