Delphi自己的关键字 [英] Delphi self keyword

查看:128
本文介绍了Delphi自己的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学Delphi阅读Marco Cantu的书,这是非常完整的。很清楚,但我对这个关键字 self 有疑问。我已经有OOP的经验,我有基础的。我的问题很简单我可以将关键字 self (Delphi)与关键字 (Java)进行比较?



当我阅读这本关于 self 的内容在记录中使用,我在我的脑海里像$ code> self:Delphi = this:Java 。看看我创建的代码进行测试:

 键入
TMarioKart =打包记录
字符:串;
卡特:字符串;
轮胎:String;
速度:双倍;
竞争:布尔;
private
air-speed:integer;
地面速度:整数;
水速:整数;
public
构造函数Create(Character:string);
函数ShowStats(a:TMarioKart):string;超载;
函数ShowStats(a:TMarioKart; b:TMarioKart):string;超载;
结束

我将切断代码的最大部分,我只是在这里显示构造函数:

 构造函数TMarioKart.Create(Character:string); 
begin
self.Character:= Character;
结束

在这里使用关键字 self 记录的字符,而不是方法中传递的字符。这是使用自己的正确方法吗?可以是Java的这个的兄弟吗?

解决方案

是的,Delphi的 Self 是Java的这个的直接模拟。


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.

这篇关于Delphi自己的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆