delphi 中的 `this` 关键字等价物
`this` keyword equivalent in delphi
假设我们有这个 class:
unit Traitement;
interface
type
TTraitement =class
public
function func1(param:String): String;
function func2(param:String): String;
end;
implementation
function TTraitement.func1(param:String): String;
begin
//some code
end;
function TTraitement.func2(param:String): String;
begin
//some code
end;
end.
而我想在func2
的代码中调用func1
。好吧,我曾经是一名 Java 程序员,在这种情况下我会使用关键字 this
。 Pascal 是否有 this
关键字的等价物?如果没有,如何实现这种调用?
相当于Java在Delphi中的this
是Self
. From the documentation:
Self
Within the implementation of a method, the identifier Self references
the object in which the method is called. For example, here is the
implementation of TCollection Add method in the Classes unit:
function TCollection.Add: TCollectionItem;
begin
Result := FItemClass.Create(Self);
end;
The Add method calls the Create method in the class referenced by the
FItemClass field, which is always a TCollectionItem descendant.
TCollectionItem.Create takes a single parameter of type TCollection,
so Add passes it the TCollection instance object where Add is called.
This is illustrated in the following code:
var MyCollection: TCollection;
...
MyCollection.Add // MyCollection is passed to the
// TCollectionItem.Create method
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
。在该代码中,您可以从 func2
调用 func1
省略 Self
.
上述文档摘录中给出的示例确实为 Self
.
的存在提供了适当的动机
假设我们有这个 class:
unit Traitement;
interface
type
TTraitement =class
public
function func1(param:String): String;
function func2(param:String): String;
end;
implementation
function TTraitement.func1(param:String): String;
begin
//some code
end;
function TTraitement.func2(param:String): String;
begin
//some code
end;
end.
而我想在func2
的代码中调用func1
。好吧,我曾经是一名 Java 程序员,在这种情况下我会使用关键字 this
。 Pascal 是否有 this
关键字的等价物?如果没有,如何实现这种调用?
相当于Java在Delphi中的this
是Self
. From the documentation:
Self
Within the implementation of a method, the identifier Self references the object in which the method is called. For example, here is the implementation of TCollection Add method in the Classes unit:
function TCollection.Add: TCollectionItem; begin Result := FItemClass.Create(Self); end;
The Add method calls the Create method in the class referenced by the FItemClass field, which is always a TCollectionItem descendant. TCollectionItem.Create takes a single parameter of type TCollection, so Add passes it the TCollection instance object where Add is called. This is illustrated in the following code:
var MyCollection: TCollection; ... MyCollection.Add // MyCollection is passed to the // TCollectionItem.Create method
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
。在该代码中,您可以从 func2
调用 func1
省略 Self
.
上述文档摘录中给出的示例确实为 Self
.