首页 / 软件开发 / Delphi / Delphi类的入门例子(4): property
Delphi类的入门例子(4): property2011-12-12 万一 //类单元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;