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."是什么意思?请举例说明。谢谢!
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."是什么意思?请举例说明。谢谢!
self.close;
TFigure.classinfo
对象的SELF
@object
如何求出对象所属类的SELF了?
pinteger(@object)
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本身!类似于一个类引用类型。至于楼主最后那句话我也有点不太明白,等待其他人的见解!
type
TTest=class
private
FA:Integer;
public
procedure SomeMethod(FA:Integer); //注意,这里申明了一个FA,但与Private里面的FA是两个不同的变量,只不过是名字相同而已!
end;
....
procedure TTest.SomeMethod(FA:Integer);
begin
FA:=100; //这个句子中的FA,编译器会认为这里是SomeMethod参数列表中的FA
//如果这里要对类的私有数据成员FA进行操作,就要使用Self.FA,而不能单独用一个FA!
end;---------------------------------------------------------------------------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's methods
begin
FA:=100; //这个句子中的FA,编译器会认为这里是SomeMethod参数列表中的FA
//如果这里要对类的私有数据成员FA进行操作,就要使用Self.FA,而不能单独用一个FA!很好理解最近声明原则。FA:Integer声明的近,所以用的是他而不是SELF.FA