Delphi Language Learning 8-Class members (fields and methods)

Source: Internet
Author: User

1.Class Fields

Example 1 member variable
Type
Tancestor = Class
Value:integer;
End
Tdescendant = Class (Tancestor)
value:string; Hides the Inherited Value field
End
Var
Myobject:tancestor;
Begin
MyObject: = tdescendant.create;
Myobject.value: = ' hello! ' Error
(MyObject as Tdescendant). Value: = ' hello! ' works!
End
Example 2 static field
Type
Tmyclass = Class
Public
class Var //Introduce a block of class static fields.
Red:integer;
Green:integer;
Blue:integer;
var //Ends the class var block.
Instancefield:integer;
End
Static fields can be called individually
Tmyclass.red: = 1;
Tmyclass.green: = 2;
Tmyclass.blue: = 3;
Static fields can also be called through object instances
Var
Myobject:tmyclass;
Myobject.red: = 1;
Myobject.green: = 2;
Myobject.blue: = 3;

2.Method Binding

Static Methods
Example 1
Type
Tfigure = Class
Procedure Draw;
End
Trectangle = Class (Tfigure)
Procedure Draw;
End
Var
Figure:tfigure;
Rectangle:trectangle;
Begin
Figure: = tfigure.create;
Figure.draw; Calls Tfigure.draw
Figure.destroy;
Figure: = trectangle.create;
Figure.draw; Calls Tfigure.draw
Trectangle (Figure). Draw; Calls Trectangle.draw
Figure.destroy;
Rectangle: = trectangle.create;
Rectangle.draw; Calls Trectangle.draw
Rectangle.destroy;
End
Virtual methods and Dynamic methods
Type
Tfigure = Class
Procedure Draw; Virtual
End
Trectangle = Class (Tfigure)
Procedure Draw; Override
End
Tellipse = Class (Tfigure)
Procedure Draw; Override
End
Var
Figure:tfigure;
Begin
Figure: = trectangle.create;
Figure.draw; Calls Trectangle.draw
Figure.destroy;
Figure: = tellipse.create;
Figure.draw; Calls Tellipse.draw
Figure.destroy;
End
Example 2
Type
T1 = Class (TObject)
Procedure Act; Virtual
End
T2 = Class (T1)
Procedure Act; The ACT is redeclared and but not overridden
End
Var
SOMEOBJECT:T1;
Begin
Someobject: = T2. Create;
Someobject.act; Calls T1. Act
End
Classes Method Class Methods
Ordinary Class Methods
The definition of a class method must begin with the reserved word class. For example,
Type
Tfigure = Class
Public
class function Supports (operation:string): Boolean; Virtual
Class procedure GetInfo (Var info:tfigureinfo); Virtual
End
The defining declaration of Class A is must also begin with class. For example,
Class procedure Tfigure.getinfo (Var info:tfigureinfo);
Begin
End
Class Static Methods
Type
Tmyclass = Class
Strict private
Class Var
Fx:integer;
Strict protected
Note:accessors for class properties must is declared class static.
class function Getx:integer; Static
Class procedure SetX (Val:integer); Static
Public
Class Property X:integer read GetX write SetX;
Class procedure Statproc (s:string); Static
End
Call
Tmyclass.x: = 17;
Tmyclass.statproc (' Hello ');

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.