Class UnitUnit person;
Interface
Uses
Dialogs;
Type
Tperson = Class (TObject)
Private
fname:string;
Fage:integer;
Public
Constructor Create (strname:string; intage:integer);
destructor Destroy; Override
function getname:string;
function Getage:integer;
Procedure SetName (const strname:string);
Procedure Setage (const intage:integer);
End
Implementation
{Tperson}
Constructor Tperson.create (strname:string; intage:integer);
Begin
Inherited Create; The Create cannot be omitted here because the parameters are different.
FName: = StrName;
If intage<0 then intAge: = 0;
Fage: = IntAge;
End
destructor Tperson.destroy;
Begin
ShowMessage (FName + ' Say hello to you! ');
Inherited Destroy;
inherited; Omitting is the method of inheriting the same name
End
function TPerson.GetName:string;
Begin
Result: = FName;
End
function TPerson.GetAge:Integer;
Begin
Result: = Fage;
End
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.Test:uses Person;
procedure TForm1.Button1Click(Sender: TObject);
var
PersonOne: TPerson;
begin
PersonOne := TPerson.Create('wy',99);
ShowMessage('姓名:' + PersonOne.GetName + '; 年龄:' +
IntToStr(PersonOne.GetAge)); //姓名:wy; 年龄:99
PersonOne.SetName('万一');
PersonOne.SetAge(100);
ShowMessage('姓名:' + PersonOne.GetName + '; 年龄:' +
IntToStr(PersonOne.GetAge)); //姓名:万一; 年龄:100
PersonOne.Free; //万一向你问好!
end;