您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Delphi >> !!! 类函数和self !!!

!!! 类函数和self !!!

来源:网络整理     时间:2016/7/17 17:42:06     关键词:

关于网友提出的“ !!! 类函数和self !!!”问题疑问,本网通过在网上对“ !!! 类函数和self !!!”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: !!! 类函数和self !!!
描述:

type
  TFigure = class
  public
    class function Supports(Operation: string): Boolean; virtual;
    class procedure GetInfo(var Info: TFigureInfo); virtual;
    ...
  end;
class procedure TFigure.GetInfo(var Info: TFigureInfo);
begin
  ...
end;
类函数Supports和类过程GetInfo与一般的函数过程有什么区别?又有什么特殊用途?
"A class method can be called through a class reference or an object reference. When it is called through an object reference, the class of the object becomes the value of Self."是什么意思呢?尤其是怎么进行class reference? 请举例说明。
self又是什么意思?怎么使用?
"Self is useful for a variety of reasons. For example, a member identifier declared in a class type might be redeclared in the block of one of the class抯 methods. In this case, you can access the original member identifier as Self.Identifier."是什么意思?请举例说明。
谢谢!


解决方案1:

类方法是在进行类申明的过程使用Class关键字进行限制的约束,是可以通过类和对象来进行调用的!类方法一般用来操作VMT中关于类本身的一些信息的存取,当然也有其他用途的!
Self按照帮助中的意思就是指定一个对象,这个对象的特点就是在这个对象里面一个方法被调用,例如下面这个方法:
procedure TForm1.OnCreate(Sender:TObject);
begin
  Self.Position:=poDeskTop;
end;
这里Self指定一个对象,并且这个对象有一个特征,就是在对象中一个方法被调用,所以这里的这个对象就是指定了Form1,而Form1的属性Position被调用,同时Position的读写方法也被调用!
这是对于一般的对象方法(即用来操作类实例的方法)而已。对于类方法,Self就相当于栈中对象实体的头四字节指针,直接指向VMT偏移地址为0的那个内存地址上,例如:
....
type
....
  TTest=class 
  ....
    class procedure ClassMethodTest;
  ....
  end;
....
procedure TTest.ClassMethodTest;
begin
  ShowMessage(Self.ClassName);
end;
这里的Self就不是类TTest的任何实例了,而直接代表类TTest本身!类似于一个类引用类型。
至于楼主最后那句话我也有点不太明白,等待其他人的见解!


以上介绍了“ !!! 类函数和self !!!”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2751120.html

相关图片

相关文章