The entry example of Delphi Class (3): Create and destroy

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.