추상·봉인 클래스
추상·봉인 클래스 (Abstract and Sealed Classes)
클래스를 sealed로 선언할 수 있어요. 그러면 그 클래스의 자손 클래스를 선언할 수 없게 됩니다. 컴파일러는 자손 선언을 만나면 오류를 돌려줘요.
본문
{$mode objfpc}
{$h+}
Type
TMyClass = Class Sealed
x : integer;
end;
TMyClass2 = Class(TMyClass)
Y : Integer;
end;
begin
end.
이 코드는 다음과 같은 오류를 냅니다.
Error: Cannot create a descendant of the sealed class "TMyClass"
추상(abstract) 클래스는 직접 인스턴스화할 수 없는 클래스예요. 대신 항상 자손 클래스를 인스턴스화해야 합니다. 다만 Delphi 호환을 위해 컴파일러는 이 지시어를 무시해요.