Mahyaa
دوشنبه 07 تیر 1389, 10:18 صبح
فرض کنیم یک کلاس داریم با نام TAClass و کلاس دیگری از کلاس اول مشتق میکنیم بنام TBClass .
کلاس TAClass یک متد در قسمت publish دارد که همین متد با نام یکسان در کلاس مشتق شده هم تعریف میشه .
حالا سوال من اینجاست که آیا باید در تعریف این متد از Virtual directive استفاده بشه یا نه.
unit Unit1;
{*
A Sample Code to test virtual dirrective usage.
I got the same result in both cases of using virtual or not using it!
*}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TAClass = class
private
A1 : Integer;
protected
A2 : string;
published
function Say(Inp : Integer) : string; virtual; //--> Is Virtual Directive required here?
end;
TBClass = class(TAClass)
public
function Say(Inp : Integer) : String; virtual; //--> Is Virtual Directive required here?
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
A : TAClass;
B : TBClass;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TAClass }
function TAClass.Say(Inp: Integer): string;
begin
ShowMessage(IntToStr(Inp));
end;
{ TBClass }
function TBClass.Say(Inp: Integer): String;
begin
ShowMessage(IntToStr(Inp * 2));
end;
//------------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
A := TAClass.Create;
B := TBClass.Create;
try
A.Say(654); //--> Result : 654
B.Say(654); //--> Result : 1308
finally
A.Free;
B.Free;
end;
end;
end.
کلاس TAClass یک متد در قسمت publish دارد که همین متد با نام یکسان در کلاس مشتق شده هم تعریف میشه .
حالا سوال من اینجاست که آیا باید در تعریف این متد از Virtual directive استفاده بشه یا نه.
unit Unit1;
{*
A Sample Code to test virtual dirrective usage.
I got the same result in both cases of using virtual or not using it!
*}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TAClass = class
private
A1 : Integer;
protected
A2 : string;
published
function Say(Inp : Integer) : string; virtual; //--> Is Virtual Directive required here?
end;
TBClass = class(TAClass)
public
function Say(Inp : Integer) : String; virtual; //--> Is Virtual Directive required here?
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
A : TAClass;
B : TBClass;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TAClass }
function TAClass.Say(Inp: Integer): string;
begin
ShowMessage(IntToStr(Inp));
end;
{ TBClass }
function TBClass.Say(Inp: Integer): String;
begin
ShowMessage(IntToStr(Inp * 2));
end;
//------------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
A := TAClass.Create;
B := TBClass.Create;
try
A.Say(654); //--> Result : 654
B.Say(654); //--> Result : 1308
finally
A.Free;
B.Free;
end;
end;
end.