Create and destroy

Source: Internet
Author: User
Code
// Class unit
Unit 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 parameter cannot be omitted 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; // The method that inherits 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 ('name: '+ personone. getname +'; age: '+
Inttostr (personone. getage); // name: WY; age: 99

Personone. setname ('case ');
Personone. setage (100 );
Showmessage ('name: '+ personone. getname +'; age: '+
Inttostr (personone. getage); // name: In case, age: 100

Personone. Free; // in case you say hello!
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.