sloppycode.net
Class example
A basic example of class creation.
Home
›
Code snippets
›
Delphi
›
Class example
For us Delphi beginners, here's an example of some of the features to use when making your own class. The uses section should obviously reflect the classes you want the class to use.
unit UnitName; { ----- Delphi Class example ----- } interface // Change the uses clause as necessary uses Windows,Messages,Classes,SysUtils; type TMyClass = class private variable1 : String; function PrivFunc(param1: integer;param2 : integer): string; procedure PrivProc(); public variable2 : String; constructor Create(); destructor Destroy(); function PubFunc():String; { var passes by reference } procedure PubProc(var param1a: String;param2a: String); protected { Protected methods/properties cannot be accessed by users of the class, but can be accessed by classes that are derived from the class } published { Published methods/properties are used when writing Delphi components. Any properties and methods put here appear in the Object Inspector at design time } end; { ----- Here is an example of inheritence ----- } // Inherits TMyClass TMyClassInherited = class(TMyClass) private // Your private methods/properties here // NOTE: If you have the derived classes' definition // in the same unit as the base class, private methods // of the original base class aren't inherited, only // protected one. public // Your public methods/properties here // and so on.... // Over-riden method procedure PubProc(var param1a: String;param2a: String); end; implementation constructor TMyClass.Create(); begin // Initialise whatever needs initialising end; procedure TMyClass.PubProc(var param1a: string;param2a: string); begin // code here end; function TMyClass.PubFunc():String; begin // code here end; function TMyClass.PrivFunc(param1: integer;param2 : integer): string; begin // code here end; procedure TMyClass.PrivProc(); begin // code here end; destructor TMyClass.Destroy(); begin // when the class is destroyed, clean up what needs cleaning. end; procedure TMyClassInherited.PubProc(var param1a: String;param2a: String); begin // Inherit all the functionality of the original method like this inherited PubProc(param1a,param2a); end; end.
{Name}
Says:
{Date}
{Text}
› Home
› C#
› Snippets
› Articles
› Tools
› Taglines
› ASP
› Dictionary Object
› FSO
› Unix cheat sheet
› Gaming
› CSS
› Yak
› Umbraco
› About
› Contact
› Privacy
› Projects
› Search
› Sitemap
Buy on Amazon
Buy on Amazon
Buy on Amazon
Buy on Amazon