I am learning Delphi reading Marco Cantu's book and it's super complete. It's very clear but I have a doubt about the keyword Self
. I already have experience with OOP and I have the basics of it. My question is very simple. Can I compare the keyword Self (Delphi) to the keyword this (Java)?
When I read on the book about the Self
used inside record, I got in my mind something like Self : Delphi = this : Java
. Look at the code I created to make a 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 am going to cut off the biggest part of the code, I am just showing the constructor here:
constructor TMarioKart.Create(Character: string);
begin
Self.Character := Character;
end;
Using the keyword Self
here I am referring to the Character of the record, and not to the Character passed in the method. Is this the correct way to use the Self? Could it be the brother of Java's this
?
Yes, Delphi's Self
is the direct analogue of Java's this
.