Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program project1;
- uses
- Classes;
- type
- TFoo = class;
- TBar = class;
- TFooListEnumerator = class
- private
- FFooList: TList;
- FCurrentIndex: Integer;
- protected
- function GetCurrent: TFoo; virtual;
- public
- constructor Create(AList: TList);
- function MoveNext: boolean;
- property Current: TFoo read GetCurrent;
- end;
- { TBarListEnumerator }
- TBarListEnumerator = class(TFooListEnumerator)
- protected
- function GetCurrent: TBar; override;
- // ^-- Fails with: Error: There is no method in an ancestor class to be overridden: "TBarListEnumerator.GetCurrent:TBar;"
- public
- property Current: TBar read GetCurrent;
- end;
- { TBarList }
- TBarList = class(TList)
- public
- function GetEnumerator: TBarListEnumerator;
- end;
- TFoo = class
- public
- Val: Integer;
- end;
- TBar = class(TFoo)
- public
- Val2: Integer;
- end;
- { TFooListEnumerator }
- function TFooListEnumerator.GetCurrent: TFoo;
- begin
- result := TFoo(FFooList[FCurrentIndex]);
- end;
- constructor TFooListEnumerator.Create(AList: TList);
- begin
- FFooList := AList;
- FCurrentIndex := -1;
- end;
- function TFooListEnumerator.MoveNext: boolean;
- begin
- inc(FCurrentIndex);
- result := FCurrentIndex < FFooList.Count;
- end;
- { TBarListEnumerator }
- function TBarListEnumerator.GetCurrent: TBar;
- begin
- result := TBar(inherited GetCurrent);
- end;
- { TBarList }
- function TBarList.GetEnumerator: TBarListEnumerator;
- begin
- result := TBarListEnumerator.Create(Self);
- end;
- var
- BarList: TBarList;
- Bar: TBar;
- begin
- BarList := TBarList.Create;
- BarList.Add(TFoo.Create);
- for Bar in BarList do
- Bar.Val2 := 1;
- BarList.Free;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement