Self 的功能和用途,可以举两个例子吗?(初学者) 关于Self 的功能,谁可举两个例子该怎么用,什么时候才出现。。本人正在学物件导向。。MSN :[email protected] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TDate = class Month, Day, Year: Integer; procedure SetValue (m, d, y: Integer); function LeapYear: Boolean; end;type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; EditYear: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TDate.SetValue (m, d, y: Integer);begin Month := m; Day := d; Year := y;end;function TDate.LeapYear: Boolean;begin // call IsLeapYear in SysUtils.pas Result := IsLeapYear (Year);end;procedure TForm1.Button1Click(Sender: TObject);var ADay: TDate;begin // create an object ADay := TDate.Create; // use the object ADay.SetValue (1, 1, StrToInt (EditYear.Text)); if ADay.LeapYear then ShowMessage ('Leap year: ' + IntToStr (ADay.Year)); // destroy the object ADay.Free;end;end.××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TDate = class Month, Day, Year: Integer; procedure SetValue (m, d, y: Integer); function LeapYear: Boolean; end;type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; EditYear: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TDate.SetValue (m, d, y: Integer);begin Self.Month := m; Self.Day := d; Self.Year := y;end;function TDate.LeapYear: Boolean;begin // call IsLeapYear in SysUtils.pas Result := IsLeapYear (Year);end;procedure TForm1.Button1Click(Sender: TObject);var ADay: TDate;begin // create an object ADay := TDate.Create; // use the object ADay.SetValue (1, 1, StrToInt (EditYear.Text)); if ADay.LeapYear then ShowMessage ('Leap year: ' + IntToStr (ADay.Year)); // destroy the object ADay.Free;end;end.这两个实例有什么分别?? 在类的普通函数中,self就是对象实例本身的一个引用。实际上,类中的普通方法都包含一个隐含的参数,这个参数就是self,也就是对象实例本身。类方法比较特殊(用class 关键字声明的类中的方法),类方法也有一个隐含的 self 参数,但是这个 self不是对象实例,而是指向类的类型信息的指针。比如:type TTestObj = class(TObject) private FA: Integer; public procedure Test1(Value: Integer); end;{ TTestObj }procedure TTestObj.Test1(Value: Integer);begin FA := Value;//这句和 Self.FA := Value 是一样的。end;上面的procedure TTestObj.Test1(Value: Integer);函数,相当于procedure TTestObj.Test1(self: TTestObj; Value: Integer); Self是指向方法的指针 在任何一个对象的方法中,都有一个隐含的变量Self。 Self是一个指针类型的变量,它指向该方法对象的实例。可以这样理解:Self是佣有该方法的对象句柄。 在普通方法中,Self变量的值是对象的引用;在一个类方法中,Self变量的值是类的引用。 Self是一个隐含的参数,是表示自身的变量,调用对象方法时,该参数便传递了过来。在方法中,相当于每个方法体中都有一个隐式的“with self do”。也就是说在方法中,所有的数据成员、方法、属性在作用域中,无需显式地引用Self就可以访问它们。它常被用于下列三个情况: 1、如果在方法内调用另外一个方法时,需要将自身参数传递过去,那么把Self传递过去好了。 2、如果派生类和基类出现了数据成员的重名,想要访问基类数据成员,而每次只以有得到派生类本身的重名数据时,就可以使用强制转换“基类名(Self).数据成员”。 3、如果在方法内调用了与数据成员同名的局部变量,则使用变量名字只以有访问到局部变量,使用“Self.变量名”,便可以访问到数据成员。 如果程序处于对象中,那么Self指向当前对象。Self作为一个内含的参数,传给每个方法。因而,在方法内部,可以通过Self引用调用该方法的对象。 简单快捷的搞明白就是function TMyType.GetVal:Integerbegin Self 写在这 Self = TMyType实例的指针 end;首先调用 TMyType.GetVal 必须声明个 TMyType 类型的变量如:vara : TMyType;begin WriteLn(a.GetVal);//TMyType.GetVal 的 Self 就是 aend;function GetVal:Integerbegin Self 写在这是错误的 因为 GetVal 不是任何类的函数end; 成员方法是类的一个属性,那么要访问这个方法就要用Self表示是“我的方法”了。这好比英语里自称就是Myself。哈哈 其实你理解了类和对象之间是一对多的关系的话,SELF就明白了一半了 菜鸟求助 如何检查局域网里的电脑是否开机?(菜鸟问题) [散分贴]mm们进来接分 请对MIDAS真正了解的朋友进来看几个问题 通过Delphi设置Execl单元格格式,怎么实现? 求‘点数字’游戏源代码=^_^= (100分)在XP的客戶端﹐程序不可用﹐出現的錯誤是"interface not supported",但是在W2K下可以 条件查询问题. 有谁遇到这种怪问题!!!!!!!!! 那有看到dll中函数的工具。 FastReport和ReportMachine如何直接引用Form的DataSet? 两个日期相减,算出间隔几小时几分几秒
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TDate = class
Month, Day, Year: Integer;
procedure SetValue (m, d, y: Integer);
function LeapYear: Boolean;
end;type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
EditYear: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.DFM}procedure TDate.SetValue (m, d, y: Integer);
begin
Month := m;
Day := d;
Year := y;
end;function TDate.LeapYear: Boolean;
begin
// call IsLeapYear in SysUtils.pas
Result := IsLeapYear (Year);
end;procedure TForm1.Button1Click(Sender: TObject);
var
ADay: TDate;
begin
// create an object
ADay := TDate.Create;
// use the object
ADay.SetValue (1, 1, StrToInt (EditYear.Text));
if ADay.LeapYear then
ShowMessage ('Leap year: ' + IntToStr (ADay.Year));
// destroy the object
ADay.Free;
end;
end.
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TDate = class
Month, Day, Year: Integer;
procedure SetValue (m, d, y: Integer);
function LeapYear: Boolean;
end;type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
EditYear: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.DFM}procedure TDate.SetValue (m, d, y: Integer);
begin
Self.Month := m;
Self.Day := d;
Self.Year := y;
end;function TDate.LeapYear: Boolean;
begin
// call IsLeapYear in SysUtils.pas
Result := IsLeapYear (Year);
end;procedure TForm1.Button1Click(Sender: TObject);
var
ADay: TDate;
begin
// create an object
ADay := TDate.Create;
// use the object
ADay.SetValue (1, 1, StrToInt (EditYear.Text));
if ADay.LeapYear then
ShowMessage ('Leap year: ' + IntToStr (ADay.Year));
// destroy the object
ADay.Free;
end;
end.这两个实例有什么分别??
实际上,类中的普通方法都包含一个隐含的参数,这个参数就是self,也就是对象实例本身。类方法比较特殊(用class 关键字声明的类中的方法),类方法也有一个隐含的 self 参数,但是这个 self不是对象实例,而是指向类的类型信息的指针。比如:
type
TTestObj = class(TObject)
private
FA: Integer;
public
procedure Test1(Value: Integer);
end;{ TTestObj }procedure TTestObj.Test1(Value: Integer);
begin
FA := Value;//这句和 Self.FA := Value 是一样的。
end;上面的
procedure TTestObj.Test1(Value: Integer);函数,相当于procedure TTestObj.Test1(self: TTestObj; Value: Integer);
Self是一个指针类型的变量,它指向该方法对象的实例。可以这样理解:Self是佣有该方法的对象句柄。
在普通方法中,Self变量的值是对象的引用;在一个类方法中,Self变量的值是类的引用。
Self是一个隐含的参数,是表示自身的变量,调用对象方法时,该参数便传递了过来。在方法中,相当于每个方法体中都有一个隐式的“with self do”。也就是说在方法中,所有的数据成员、方法、属性在作用域中,无需显式地引用Self就可以访问它们。它常被用于下列三个情况:
1、如果在方法内调用另外一个方法时,需要将自身参数传递过去,那么把Self传递过去好了。
2、如果派生类和基类出现了数据成员的重名,想要访问基类数据成员,而每次只以有得到派生类本身的重名数据时,就可以使用强制转换“基类名(Self).数据成员”。
3、如果在方法内调用了与数据成员同名的局部变量,则使用变量名字只以有访问到局部变量,使用“Self.变量名”,便可以访问到数据成员。
如果程序处于对象中,那么Self指向当前对象。Self作为一个内含的参数,传给每个方法。因而,在方法内部,可以通过Self引用调用该方法的对象。
begin
Self 写在这 Self = TMyType实例的指针
end;首先调用 TMyType.GetVal 必须声明个 TMyType 类型的变量
如:
var
a : TMyType;
begin
WriteLn(a.GetVal);//TMyType.GetVal 的 Self 就是 a
end;function GetVal:Integer
begin
Self 写在这是错误的 因为 GetVal 不是任何类的函数
end;