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
?
TMarioKart.Create(ACharacter: string);
- note theA
for the argument. and avoid theself
. – MontagnardA := B
whereA
andB
are of typeTMarioKart
. With records you will copy the value, with classes you will copy the reference. – Demolish-
with variable/field names. (i.e.air-speed
)... – MontagnardA
prefix convention for parameters. There is no need for that, because class/record members already have aF
prefix. The RTL does not use that convention anymore, see: docwiki.embarcadero.com/Libraries/XE6/en/System.SysUtils.Format and many thousands of other examples. – Vanwardconst
, if that is possible. Andair-speed
, etc. do indeed use the minus character, which is not allowed in Delphi. He could useair_speed
but I would rather write FAirSpeed, in line with another convention. – Discursivethis
, though. – Discursiveobject
types, the forerunners of theclass
types as introduced in Delphi (1.0). – Discursive