::::::::======reintroduce地用法=======::::::::(300分)

C

cosy

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }


public
{ Public declarations }
end;
type
Tfather=class
public
function draw:string;virtual;
end;
Tchild=class(Tfather)
public

################################################

//function draw:string;override;
//function draw:string;
function draw:string;reintroduce;
#################################################

end;
var
Form1: TForm1;



implementation

{$R *.dfm}
function Tfather.draw;
begin
showmessage('Tfather.draw');
end;
function Tchild.draw;
begin
showmessage('Tchild.Draw');
end;
procedure TForm1.Button1Click(Sender: TObject);
var father:Tfather;

begin
father:=Tfather.Create;
father.draw;

end;

procedure TForm1.Button2Click(Sender: TObject);
var child:Tchild;


child:=Tchild.Create;
child.draw;
end;

end.


这里的reintroduce有什么作用?(我知道他是隐藏祖先类的虚方法)具体如何应用
怎么使用呢?


 
如果你的子类一个方法想用父类的同样名字的方法,又不想让方法有override关系,
那就只有用reintroduce,就是说我子类的这个方法与父类是没有关系的,只是名字相同
有点类似于同一个类里面的名字相同的很多方法的overload

-----
http://www.8421.org
 
假如,你在写子类时,新声明了一个跟父类中已有的方法同名的方法,这样编译器将会自动
对父类该方法进行隐藏!
当父类中的方法为普通静态方法时,无所谓的,你只要在子类中直接声明定义你的同名方法
即可,编译器对于这种隐藏不会有什么反应!但是,当父类中该方法为虚拟或动态方法时,
你在子类中所声明并定义的同名方法,编译器将会报警说你隐藏了基类中的同名虚拟方法!
所以,假如你确定要这么做,而又不想让编译器进行报警的话,就要用到reintroduce,说
白了,它的用法就是去镇压编译器的报警的!
 
我们知道
override把子类的方法付给父类 使父类的调用也可以有子类的特性

如果我把父类的虚方法在子类中reintroduce一下,那么子类的这个同名方法和父类的虚方法
(动态方法)是什么关系?和override有相似之处吗?
 
接受答案了.
 
override关键字的作用是为了在程序编码时实现多态性,通过覆盖父类的虚拟或动态方法来
实现之。不是像你理解的那样。假如,你把父类的虚方法在子类中reintroduce一下的话,这
个子类中的同名方法将只会隐藏来自父类中的同名虚方法,并不会实现多态性!
它和override是两个不同的概念:override是用于实现多态性的关键字,而reintroduce
正如我上一贴所述那样,只不过是用于镇压编译器报警的,就算你不用它也不打紧的,程序仍
然可以正常运行!
 
顶部