//类单元 unit Person; interface type TPerson = class(TObject) private FName: string; FAge: Integer; public procedure SetName(const strName: string); procedure SetAge(const intAge: Integer); property Name: string read FName write SetName; property Age: Integer read FAge write SetAge; end; implementation { TPerson } procedure TPerson.SetName(const strName: string); begin FName := strName; end; procedure TPerson.SetAge(const intAge: Integer); begin if intAge<0 then FAge := 0 else FAge := intAge; end; end.
//测试: uses Person; procedure TForm1.Button1Click(Sender: TObject); var PersonOne: TPerson; begin PersonOne := TPerson.Create; PersonOne.Name := '万一'; PersonOne.Age := 100; ShowMessage('姓名:' + PersonOne.Name + '; 年龄:' + IntToStr(PersonOne.Age)); //姓名:万一; 年龄:100 PersonOne.Free; end;