Keyword Delphi

I study Delphi, read a book by Marco Cant, and it is super complete. This is very clear, but I doubt the keyword self . I already have experience with OOP, and I have the basics of this. My question is very simple. Can I compare the keyword self (Delphi) with the keyword this (Java)?

When I read the book on self used internal records, I realized something like self : Delphi = this : Java . Take a look at the code I created for the test:

 type TMarioKart = packed record Character: String; Kart: String; Tires: String; Speed: double; Competitive: boolean; private air-speed: integer; ground-speed: integer; water-speed: integer; public constructor Create(Character: string); function ShowStats(a: TMarioKart):string; overload; function ShowStats(a: TMarioKart; b: TMarioKart): string; overload; end; 

I'm going to cut off most of the code, I just show the constructor here:

 constructor TMarioKart.Create(Character: string); begin self.Character := Character; end; 

Using the self keyword here, I am referring to the record character, not the character passed in in the method. Is it right to use yourself? Could this be Java's brother this ?

+9
delphi self
source share
3 answers

Yes, Delphi Self is a direct analog of Java this .

+2
source share

Self very similar to this in Java, C ++, or C #. However, this is called a little more, as the following code shows.

In Delphi, you can have class methods that are not static, but also have a Self pointer, which obviously does not point to an instance of the class, but the type of the class itself that calls the method.

See the output of this program:

 program WhatIsSelf; {$APPTYPE CONSOLE} type TAnimal = class procedure InstanceMethod; class procedure ClassMethod; class procedure StaticClassMethod; static; end; TDog = class(TAnimal) end; class procedure TAnimal.ClassMethod; begin Writeln(Self.ClassName); end; procedure TAnimal.InstanceMethod; begin Writeln(Self.ClassName); end; class procedure TAnimal.StaticClassMethod; begin // Writeln(Self.ClassName); // would not compile with error: E2003 Undeclared identifier: 'Self' end; var t: TAnimal; begin t := TDog.Create; t.InstanceMethod; t.ClassMethod; TAnimal.ClassMethod; TDog.ClassMethod; Readln; end. 
+13
source share

Formally speaking, Self is a normal identifier (it is automatically declared in some cases).

Self automatically determined in the implementation of the methods and methods of the class, and its purpose there is similar to this in Java, as already mentioned. The underlying technology is similar to the Result variable in normal functions.

In other words, there is no self keyword (which is why it is usually written in upper case S ). When you want (and you can [*] ), you can enter your own variable, method or class Self .

 type // TForm1 ommitted Self = class function Self: Integer; end; function Self.Self: Integer; begin Result := SizeOf(Self); end; 

Of course, you will have difficulty creating an instance of an object of this type in the method, in these cases you will have to use it through the adapter function (or procedure):

 function UseSelf: Integer; var S: Self; begin S := Self.Create; Result := S.Self; S.Free; end; function VarSelf: Integer; var Self: Integer; begin Self := SizeOf(Result); Result := Self; end; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(IntToStr(UseSelf)+' '+IntToStr(VarSelf)); end; 

[*] You cannot declare a Self variable in methods, because such a declaration already exists and this is exactly what the error says:

Identifier redeclared: 'Self'

0
source share

All Articles