delphi 中 virtual、reintroduce、override关键词的使用

虚拟方法和动态方法不同于静态方法,它们可以在其后裔类中被覆盖(overridden),即只有一个方法所在类的祖先类对应的方法是virtual或dynamic的,这个当前类的方法才可以用overide覆盖。当一个覆盖方法被调用时,方法调用中使用的类或对象的实际(运行时)类型决定了哪一个实现是有效的,而非变量声明的类型决定。virtual方法是可以有实现的部分的,也可是在后裔类中override。

abstract抽象方法是那些在类中声明但未实现的虚拟方法或动态方法。抽象方法的实现推延到后裔类中。声明抽象方法必需在指示字virtual或dynamic之后使用abstract。例如:procedure DoSomething; virtual; abstract;  即abstract方法是没有实现的,实现推延到后裔类中,后裔override这个方法后可以有实现部分。


如果:
TBaseObj=class
  public
    procedure AMethod;
  end;

TChildObj=class(TBaseObj)
    procedure AMethod;
  end;

{ TBaseObj }

procedure TBaseObj.AMethod(str:string);
begin
  showmessage('TBaseObj.AMethod;');
end;

{ TObj }

procedure TChildObj.AMethod(str:string);
begin
  showmessage('TChildObj.AMethod;');
end;

实际上两个类的AMethod方法无任何关系。子类中将看不到祖先类的AMethod方法,而只有自已的AMethod方法,运行下面事件过程将执行
 showmessage('TChildObj.AMethod;');

procedure TForm1.Button2Click(Sender: TObject);
var
  childobj:TChildObj;
begin
  childobj:=TChildObj.Create;
  childobj.AMethod;

end;

如果

TBaseObj=class
  public
    procedure AMethod(str:string;i:integer=0);virtual;
  end;

TChildObj=class(TBaseObj)
    procedure AMethod(str:string);
  end;


pprocedure TBaseObj.AMethod(str:string;i:integer=0);
begin
  showmessage('Base'+str);
end;

{ TObj }

procedure TChildObj.AMethod(str:string;i:integer=0);
begin
  showmessage('Child'+str);
end;

执行下面事件过程,将看到showmessage中显示'ChildAMethod;'  并有[Warning] Unit1.pas(28): Method 'AMethod' hides virtual method of base type 'TBaseObj'的编译信息显示,说明在子类没有用override关键字时,声明一个与祖先类同名的方法是会隐藏祖先类的同名方法,执行自已的方法。而在子类方法声明后加reintroduce就不会有编译信息显示。如果使用override,要保证子类与祖先类方法的参数类型,顺序返回值相同,不然会在编译时报错:
因此交上面子类声明改为:procedure AMethod(str:string;i:integer=0);override;,实现部分也要相应更改,这样执行下面事件过程,即子类方法覆盖了祖先类方法最后将看到showmessage中显示'ChildAMethod;' ,如果有多个子类,都override那个virtual的祖先类的方法,那么最后执行结果是子类的这个AMethod方法,覆盖了祖先类同名方法的实现内容。实际上如果祖先类的这个方法是virtual;abstract的结果也是一样的,。即祖先类只定义一个声明,具体实现由这个祖先类的子类们去完成,执行不同子类结果不同,因为各子类的AMethod实现细节都有所不同。

procedure TForm1.Button2Click(Sender: TObject);
var
  childobj:TChildObj;
begin
  childobj:=TChildObj.Create;
  childobj.AMethod('AMethod;');

end;

另一个有用的东西是:inherited关键字,如果将上面的子类方法实现改为:
procedure TChildObj.AMethod(str:string;i:integer=0);
begin
  inherited;
  showmessage('Child'+str);
end;
然后再运行Button2Click,则会先看到showmessage出BaseAMethod,再看到showmessage出ChildAMethod。为什么会有这种现象呢,看一下关于inherited的说明:
  保留字inherited在实现多种行为中扮演特殊的角色。它可以出现在方法定义中,在其后面可以有或没有标识符。
如果inherited之后跟随一个成名名称,那么除了表示在封装了方法的类的直接祖先中搜寻成员之外,还可以表示标准的方法调用或者对属性或域的引用。

也就是说运行到inherited时会告诉当前inherited所在方法中祖先类的同名方法,所以先看到showmessage出BaseAMethod。如果祖先类中有一个abc方法,那么在此也可调用,即 inherited abc,即如果inherited后面没参数,执行时找祖先类同名方法,有参数如abc则在祖先类找那个
abc方法。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
I stopped working on this project for three reasons; 1) I had to focus on my main project. This was just a fun side project. 2) I didn't feel comfortable abusing copyrighted material for non-personal use, and neither should you. 3) The competition this was an entry for ended. I'm releasing the source code for this project since I've gotten quite a bit of email with really good suggestions on how to make this better, but I don't have the time to implement them myself. The code (/src/) is released as public domain, so you can do with it what you wish. The art (/res/) is still copyright nintendo, so it's almost certainly NOT ok to do anything at all with it. Ask nintendo. And, please, if you're going to make a bigger project out of this, please consider replacing the art with legal art. About the code: The code is basically undocumented, but should be readable anyway as it's fairly clean. The main entry points are AppletLauncher and FrameLauncher. The main game is in MarioComponent. "sonar" is the base of a software sound engine I've been working on. It's pretty nice, but there's a few bugs in it (mostly timing based). It can easilly be ripped out and reused in another project. The level editor isn't used for anything more than changing the behavior of blocks anymore. But I think there's still code in there somewhere for loading a level instead of generating it, so if you want to reintroduce static levels, you've got a nice base for a level editor there. The game DOES support scrolling in the Y directions right out of the box! However, I didn't think it fit the retro feeling, so I made all levels only one screen tall. ;) The sprite package and class should be renamed into "entity" or "mobile" or something..
DELPHI方法的类型及其覆盖、重载 1、静态方法是方法的缺省类型,对它就像对通常的过程和函数那样调用,编译器知道这些方法的地址,所以调用一个静态方法时它能把运行信息静态地链接进可执行文件,所以,静态方法速度最快,但它们却不能被覆盖来支持多态性。 2、虚拟方法和静态方法的调用方式相同。由于虚拟方法能被覆盖,在代码调用一个指定的虚拟方法时编译器并不知道它的地址,因此,编译器通过建立虚拟方法表(VMT)来查找在运行时的函数地址。所有的虚拟方法在运行时通过VMT来高度,一个对象的VMT表除了自己定义的虚拟方法外,还有它的祚的所有的虚拟方法,因此虚拟方法比动态方法用的内存要多,但它执行得比较快。 3、动态方法跟虚拟方法基本相似,只是它们的高度系统不同。编译器为每一个动态方法指定一个独一无二的数字,用这个数字和动态方法的地址构造一个动态方法表(DMT)。不像VMT表,在DMT表仅有它声明的动态方法,并且这个方法需要祖先的DMT表来访问它蓁的动态方法。正因为这样动态方法比虚拟方法用的内存要少,但执行起来罗慢,因为有可能要到祚对象的DMT查找动态法。 4、OP通过覆盖使一方法在不同的派生类间表现出不同的行为。OP能被覆盖的方法是在声明时被标识为virtual或dynamic的方法。为了覆盖一个方法,在派生类的声明override代替virtual或dynamic。用了override后,编译器就会用新的方法替换VMT原先的方法,而原先的方法也还存在,而如果用override再次声明一个静态方法,则是真正的覆盖,是用新的方法完全替换在祖先类的同明方法。 5、方法重载的意义:比如说,你要写一个求两数商的函数(当然只是个比喻),你希望这个函数可以处理所有的数值类型,但PASCAL的运算对类型实行严格检查,你不得不用不同的程序来运算不同类型的数值,这样你就必须为每一种类型写一个同样功能的函数,并使用不同的函数名,坏处我想你是知道的。而重载却可以解决这个问题,同样的函数名,编译器可以用不同的形参类型决定调用哪个函数。Top dynamic和virtual的不同之处 delphi动态虚拟覆盖重载重定义的区别 (www.ip8000.com www.sql8.net)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值