Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
Delphi 10.4 Sydney
|
AW: unterschiedliche Klassen variabel instanzieren
21. Jul 2011, 21:57
Meinst du so ?
Delphi-Quellcode:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TKlasse = class(TObject)
public
constructor Create( const ATyp: integer); overload;
constructor Create; overload;
end;
TKlasse1 = class(TKlasse)
function MachWas: string;
end;
TKlasse2 = class(TKlasse)
function MachWas: string;
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Machwas( const ATyp:integer);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Machwas(1);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Machwas(2);
end;
procedure TForm1.Machwas( const ATyp:integer);
var
Klasse: TKlasse;
begin
if ATyp=1 then
begin
Klasse:=TKlasse.Create(ATyp);
try
with Klasse as TKlasse1 do ShowMessage(Machwas);
finally
Klasse.Free;
end;
end;
if ATyp=2 then
begin
Klasse:=TKlasse.Create(ATyp);
try
with Klasse as TKlasse2 do ShowMessage(Machwas);
finally
Klasse.Free;
end;
end;
end;
constructor TKlasse.Create;
begin
inherited Create;
end;
constructor TKlasse.Create( const ATyp: integer);
begin
if ATyp=1 then Self:= TKlasse1.Create;
if ATyp=2 then Self:= TKlasse2.Create;
end;
function TKlasse1.MachWas: string;
begin
Result:= ' TKlasse1.MachWas';
end;
function TKlasse2.MachWas: string;
begin
Result:= ' TKlasse2.MachWas';
end;
end.
|
|
Zitat
|