Hallo,
ich möchte meine Klassen mit Hilfe des Parameters im virtuellen Konstruktor erzeugen.
folgendes Beispiel:
Delphi-Quellcode:
unit MyInterface;
interface
uses
Dialogs;
type
TApeClass =
class of TApe;
TApeType = TApeClass;
TApe =
class (TObject)
public
class function CreateApe(
const ApeType : TApeType): TApe;
procedure Scream;
virtual;
end;
TGorrila =
class (TApe)
public
procedure Scream;
override;
end;
TOrangutan =
class (TApe)
public
procedure Scream;
override;
end;
TChimp =
class (TApe)
public
procedure Scream;
override;
end;
implementation
{- TApe.CreateApe }
{
************************************* TApe *************************************
}
{- TApe.CreateApe }
class function TApe.CreateApe(
const ApeType : TApeType): TApe;
begin
Result := TApeType.Create;
end;
{- TApe.Scream }
procedure TApe.Scream;
begin
end;
{- TApe.Scream }
{- TGorrila.Scream }
{
*********************************** TGorrila ***********************************
}
{- TGorrila.Scream }
procedure TGorrila.Scream;
begin
Showmessage('
I am a gorrila!')
end;
{- TOrangutan.Scream }
{
********************************** TOrangutan **********************************
}
{- TOrangutan.Scream }
procedure TOrangutan.Scream;
begin
showmessage('
I am an Orangutan!');
end;
{- TChimp.Scream }
{
************************************ TChimp ************************************
}
{- TChimp.Scream }
procedure TChimp.Scream;
begin
Showmessage('
I am a Chimp!');
end;
end.
//Aufruf im Programm:
...
var
Ape: TApe;
begin
Ape:= TApe.CreateApe(TChimp);
Ape.Scream;
end;
So wird immer TApe.Scream aufgerufen... Was mache ich falsch?