self 是什么?怎么用?

Fantasiazhang 2003-01-21 01:23:37
self 是什么?怎么用?
...全文
121 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
brallow 2003-05-11
  • 打赏
  • 举报
回复
看样子我应该收藏一下这一页了,呵呵。
cosda99 2003-05-11
  • 打赏
  • 举报
回复
是一个THANDLE指针
chep 2003-05-09
  • 打赏
  • 举报
回复
Methods are very similar to procedures and functions. The real difference is that methods have an implicit parameter, which is
a reference to the current object.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Within a method you can refer to this parameter—the current object—using the Self keyword. This extra hidden parameter is needed when you create several objects of the same class, so that each time you apply a method to one of the objects, the method will operate only on its own data and not affect sibling objects.
For example, in the SetValue method of the TDate class, listed earlier, we simply use Month, Year, and Day to refer to the fields of the current object, something you might express as
Self.Month := m;
Self.Day := d;
This is actually how the Delphi compiler translates the code, not how you are supposed to write it. The Self keyword is a fundamental language construct used by the compiler, but at times it is used by programmers to resolve name conflicts and to make tricky code more readable.
NOTE The C++ and Java languages have a similar feature based on the keyword this.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
alusa 2003-01-28
  • 打赏
  • 举报
回复
Self 用于类的方法代码中,表示调用方法的实例,Self 是内置在编译器中的,不论是从哪一级类中继承的都可以使用,包括用object关键字定义的不从TObject继承的类。
dixin01 2003-01-28
  • 打赏
  • 举报
回复
procedure TForm1.Button2Click(Sender:TObject);
var
B1,B2:TButton;
begin
B1:=TButton.Create(self);
B2:=TButton.Create(B1);//B1为B2的宿主(Owner)
B2.Free;
B1.Free;
end;
flyingbar 2003-01-26
  • 打赏
  • 举报
回复
self变量是对象中系统隐含的一个成员变量,每个类都有,指代的是对象本身。
写self.IsOn和直接写IsOn是完全一样的。因为一个方法在使用到一个变量时,会在类的成员变量中查找,所以不用明确地指出作用域。
Fantasiazhang 2003-01-23
  • 打赏
  • 举报
回复
那么在form1.formcreate过程中进行button1:=tbutton.create(self)是为了什么?这里的self是不是指form1呢?这样写有什么作用那?
anux 2003-01-23
  • 打赏
  • 举报
回复
Sender--意义:指本对象。Sender在什么对象相关代码里,那么Sender就是什么。
Self--意义:指本类,也就是Self被引用的类。比如若在类TMyClass内引用了Self,那么Self=TMyClass.
Owner--意义:哪个对象释放我的内存啊?

如:Pan:=TPanel.Create(Self);其中Create的参数是:AOwner:TComponent。Owner释放Pan的内存。因为窗口释放Pan的内存,但窗口类的对象是Self.一般给Owner传Self就可以。
比如:
代码段一:
pan:=TPanel.Create(Self);
with Pan do begin
try
Left:=20;
Top:=20;
parent:=Self; //Parent:=Form1也可以。
Visible:=true;
ShowMessage('Created');
finally
Pan.free;
end;
end;
-----------------------------------------------------
Parent--
意义:此对象包括在哪个对象里那?
说明:若组件不是从TControl继承来的,那么在创建组件后不必声明此属性。
westfly 2003-01-23
  • 打赏
  • 举报
回复
指定owner为form1,这样当form1被销毁时button1也会自动被销毁。
top_science2000 2003-01-22
  • 打赏
  • 举报
回复
self 是对应于C++中的this. self 其实就是指向对象自己的一个指针。一个对象在调用自己的方法时,缺省将指向自己的指针(也就是self)传递到方法中。因此在方法中使用Self,可以代表对象自己。
mopyman 2003-01-22
  • 打赏
  • 举报
回复
对应C++中的this
richard_ouyang 2003-01-22
  • 打赏
  • 举报
回复
self是当前类本身,但是只有从Tcomponent继承下来的类才有,自己的是没有的
snake_eye 2003-01-22
  • 打赏
  • 举报
回复
self只可议会不可言喘了
form1里 self.button1=form1.button1
在写构件的时候Self就是你要写的构件本身 如:self.fset=mybuton.fset;等
其实就是指你的代码所在的函数的类。
gzyzljk 2003-01-22
  • 打赏
  • 举报
回复
指本类,也就是Self被引用的类。比如若在类TMyClass内引用了Self,那么Self=TMyClass
小武的没错的......
wuwoczj 2003-01-22
  • 打赏
  • 举报
回复
有时因为写了self会引起句柄的错误,
如需要传递本窗口的句柄可以直接写Handle,而不可以写Self.Handle否则
将可能引起一系列的问题
webzhangtao 2003-01-21
  • 打赏
  • 举报
回复
self是在类方法中使用的指向类自身的指针
如procedure TForm1.Button1Click(Sender: TObject);
begin
self.Caption:='ss';
end;
你当然也可写成
procedure TForm1.Button1Click(Sender: TObject);
begin
Form1.Button1.Caption:='ss';
end;
但这样太麻烦了
stargazer 2003-01-21
  • 打赏
  • 举报
回复
西翔对,
小武错.
fancier 2003-01-21
  • 打赏
  • 举报
回复
self 是指本类。也就是self被引用的类。
比如在类TMyclass中引用self,那么self=TMyClass
westfly 2003-01-21
  • 打赏
  • 举报
回复
用于类的方法代码中,表示自身所在实例。

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧