출력은클래스 참조를 사용한 다형성 및 상속? 아래의 콘솔 응용 프로그램의
Parent
Parent
Parent
대신
Parent
Child1
Child2
왜 이런 일이 것입니까? 또한, 의도 된 출력을 얻는 방법? 많은 감사합니다!
PS : 아직 단서가이 related SO post ...
program Project1;
{$APPTYPE CONSOLE}
type
TParent = class;
TParentClass = class of TParent;
TParent = class
public
ID: string;
constructor Create;
end;
TChild1 = class(TParent)
public
constructor Create;
end;
TChild2 = class(TParent)
public
constructor Create;
end;
constructor TParent.Create;
begin
ID := 'Parent';
end;
constructor TChild1.Create;
begin
ID := 'Child1';
end;
constructor TChild2.Create;
begin
ID := 'Child2';
end;
procedure Test(ImplClass: TParentClass);
var
ImplInstance: TParent;
begin
ImplInstance := ImplClass.Create;
WriteLn(ImplInstance.ID);
ImplInstance.Free;
end;
begin
Test(TParent);
Test(TChild1);
Test(TChild2);
Readln;
end.
참고 코드를 편집했습니다. 'TParent'에서 선언 한 것을 숨기는 파생 된 각 클래스에 새로운'ID' 필드를 추가하는 것을 의미하지 않았습니다. –