Skip to content
Delphi

Unit 与类

定义带 interface 和 implementation 区段的 unit。

#unit#class#oop

Code

delphi
unit MyUnit;

interface

type
  TPerson = class
  private
    FName: string;
    FAge: Integer;
  public
    constructor Create(const Name: string; Age: Integer);
    destructor Destroy; override;
    property Name: string read FName write FName;
    property Age: Integer read FAge write FAge;
    function ToString: string; override;
  end;

implementation

constructor TPerson.Create(const Name: string; Age: Integer);
begin
  inherited Create;
  FName := Name;
  FAge := Age;
end;

destructor TPerson.Destroy;
begin
  // cleanup
  inherited;
end;

function TPerson.ToString: string;
begin
  Result := Format('%s (%d)', [FName, FAge]);
end;

end.