Einzelnen Beitrag anzeigen

Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#10

AW: Vererbung und Polymorphie

  Alt 18. Dez 2014, 13:28
Ich würde vorschlagen, du verlegst die Adds und Castings weitegehend in die Liste, dann tust du dir anschließend leichter. Mit Generics geht das wahrscheinlich noch einfacher (keine Ahnung, hab D2007).
Delphi-Quellcode:
procedure TMenschTestForm.SomeButtonClick(Sender: TObject);
var
  Menschen: TMenschen;
  Index: integer;
begin
  Menschen := TMenschen.Create;
  try
    Index := Menschen.AddSportler;
    Menschen.Sportler[Index].SomeProp := 'Fußball';
    ShowMessage(IntToStr(Menschen.SportlerCount));
  finally
    Menschen.Free;
  end;
end;
Delphi-Quellcode:
  TMenschen = class(TObjectList)
  private
    function GetArbeiter(Index: integer): TArbeiter;
    function GetAzubi(Index: integer): TAzubi;
    function GetJob(Index: integer): TJob;
    function GetMensch(Index: integer): TMensch;
    function GetSchueler(Index: integer): TSchueler;
    function GetSchule(Index: integer): TSchule;
    function GetSportler(Index: integer): TSportler;
    function GetArbeiterCount: integer;
    function GetAzubiCount: integer;
    function GetJobCount: integer;
    function GetMenschCount: integer;
    function GetSchuelerCount: integer;
    function GetSchuleCount: integer;
    function GetSportlerCount: integer;
    function GetIsArbeiter(Index: integer): boolean;
    function GetIsAzubi(Index: integer): boolean;
    function GetIsJob(Index: integer): boolean;
    function GetIsMensch(Index: integer): boolean;
    function GetIsSchueler(Index: integer): boolean;
    function GetIsSchule(Index: integer): boolean;
    function GetIsSportler(Index: integer): boolean;
  public
    function AddMensch: integer;
    function AddJob: integer;
    function AddSchule: integer;
    function AddArbeiter: integer;
    function AddSportler: integer;
    function AddAzubi: integer;
    function AddSchueler: integer;

    property MenschCount: integer read GetMenschCount;
    property JobCount: integer read GetJobCount;
    property SchuleCount: integer read GetSchuleCount;
    property ArbeiterCount: integer read GetArbeiterCount;
    property SportlerCount: integer read GetSportlerCount;
    property AzubiCount: integer read GetAzubiCount;
    property SchuelerCount: integer read GetSchuelerCount;

    property Mensch[Index: integer]: TMensch read GetMensch;
    property Job[Index: integer]: TJob read GetJob;
    property Schule[Index: integer]: TSchule read GetSchule;
    property Arbeiter[Index: integer]: TArbeiter read GetArbeiter;
    property Sportler[Index: integer]: TSportler read GetSportler;
    property Azubi[Index: integer]: TAzubi read GetAzubi;
    property Schueler[Index: integer]: TSchueler read GetSchueler;

    property IsMensch[Index: integer]: boolean read GetIsMensch;
    property IsJob[Index: integer]: boolean read GetIsJob;
    property IsSchule[Index: integer]: boolean read GetIsSchule;
    property IsArbeiter[Index: integer]: boolean read GetIsArbeiter;
    property IsSportler[Index: integer]: boolean read GetIsSportler;
    property IsAzubi[Index: integer]: boolean read GetIsAzubi;
    property IsSchueler[Index: integer]: boolean read GetIsSchueler;
  end;

implementation

{ TMenschen }

function TMenschen.AddArbeiter: integer;
begin
  Result := Add(TArbeiter.Create);
end;
function TMenschen.AddAzubi: integer;
begin
  Result := Add(TAzubi.Create);
end;
function TMenschen.AddJob: integer;
begin
  Result := Add(TJob.Create);
end;
function TMenschen.AddMensch: integer;
begin
  Result := Add(TMensch.Create);
end;
function TMenschen.AddSchule: integer;
begin
  Result := Add(TSchule.Create);
end;
function TMenschen.AddSchueler: integer;
begin
  Result := Add(TSchueler.Create);
end;
function TMenschen.AddSportler: integer;
begin
  Result := Add(TSportler.Create);
end;

function TMenschen.GetArbeiter(Index: integer): TArbeiter;
begin
  if IsArbeiter[Index] then
    Result := TArbeiter(Items[Index])
  else
    raise Exception.Create('GetArbeiter');
end;
function TMenschen.GetAzubi(Index: integer): TAzubi;
begin
  if IsAzubi[Index] then
    Result := TAzubi(Items[Index])
  else
    raise Exception.Create('GetAzubi');
end;
function TMenschen.GetSchueler(Index: integer): TSchueler;
begin
  if IsSchueler[Index] then
    Result := TSchueler(Items[Index])
  else
    raise Exception.Create('GetSchueler');
end;
function TMenschen.GetJob(Index: integer): TJob;
begin
  if IsJob[Index] then
    Result := TJob(Items[Index])
  else
    raise Exception.Create('GetJob');
end;
function TMenschen.GetMensch(Index: integer): TMensch;
begin
  if IsMensch[Index] then
    Result := TMensch(Items[Index])
  else
    raise Exception.Create('GetMensch');
end;
function TMenschen.GetSchule(Index: integer): TSchule;
begin
  if IsSchule[Index] then
    Result := TSchule(Items[Index])
  else
    raise Exception.Create('GetSchule');
end;
function TMenschen.GetSportler(Index: integer): TSportler;
begin
  if IsSportler[Index] then
    Result := TSportler(Items[Index])
  else
    raise Exception.Create('GetSportler');
end;

function TMenschen.GetArbeiterCount: integer;
var
  I: integer;
begin
  Result := 0;
  for I := 0 to Count - 1 do
    if IsArbeiter[I] then
      Inc(Result);
end;
function TMenschen.GetAzubiCount: integer;
var
  I: integer;
begin
  Result := 0;
  for I := 0 to Count - 1 do
    if IsAzubi[I] then
      Inc(Result);
end;
function TMenschen.GetJobCount: integer;
var
  I: integer;
begin
  Result := 0;
  for I := 0 to Count - 1 do
    if IsJob[I] then
      Inc(Result);
end;
function TMenschen.GetMenschCount: integer;
var
  I: integer;
begin
  Result := 0;
  for I := 0 to Count - 1 do
    if IsMensch[I] then
      Inc(Result);
end;
function TMenschen.GetSchuleCount: integer;
var
  I: integer;
begin
  Result := 0;
  for I := 0 to Count - 1 do
    if IsSchule[I] then
      Inc(Result);
end;
function TMenschen.GetSchuelerCount: integer;
var
  I: integer;
begin
  Result := 0;
  for I := 0 to Count - 1 do
    if IsSchueler[I] then
      Inc(Result);
end;
function TMenschen.GetSportlerCount: integer;
var
  I: integer;
begin
  Result := 0;
  for I := 0 to Count - 1 do
    if IsSportler[I] then
      Inc(Result);
end;

function TMenschen.GetIsArbeiter(Index: integer): boolean;
begin
  Result := Items[Index] is TArbeiter;
end;
function TMenschen.GetIsAzubi(Index: integer): boolean;
begin
  Result := Items[Index] is TAzubi;
end;
function TMenschen.GetIsJob(Index: integer): boolean;
begin
  Result := Items[Index] is TJob;
end;
function TMenschen.GetIsMensch(Index: integer): boolean;
begin
  Result := Items[Index] is TMensch;
end;
function TMenschen.GetIsSchule(Index: integer): boolean;
begin
  Result := Items[Index] is TSchule;
end;
function TMenschen.GetIsSchueler(Index: integer): boolean;
begin
  Result := Items[Index] is TSchueler;
end;
function TMenschen.GetIsSportler(Index: integer): boolean;
begin
  Result := Items[Index] is TSportler;
end;

Geändert von Bjoerk (18. Dez 2014 um 13:43 Uhr) Grund: Menschen.Sportler[Index].SomeProp := 'Fußball';
  Mit Zitat antworten Zitat