13
Delphi Classes – roll your own Delphi classes Modify to change visibility of properties Inherit from others Add extra specialised behaviour Create your own Create completely new classes Customer Preferences Connection

Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Embed Size (px)

Citation preview

Page 1: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Delphi Classes – roll your own

Delphi classes Modify to change visibility of properties

Inherit from others Add extra specialised behaviour

Create your own Create completely new classes

Customer Preferences Connection

Page 2: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Delphi Classes - scope Delphi classes

Private Visible within the class

Protected Visible to ‘children’ of the class

Public Visible to all users of the class

Published Visible properties to all users of the class

Page 3: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Delphi Classes – modify Classes have certain properties made

visible Every class has a custom version –

with no properties visible Use this to decide which fields or

attributes you wish to promote to published properties

Can only make more visible in sub-classes not more private

Page 4: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Inherit from others Inherit from the custom version

TCustomListBox TCustomComboBox TCustomADODataSet etc..

Add extra fields or methods Decide which fields are to be

published Still have access to all the built in

behaviour

Page 5: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Create your own classes Create completely new classes

Descend from appropriate base class Control Graphic List etc.,

Or from the most basic - TObject

Page 6: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Create your own – Syntax 1 Help, Object Pascal Language Guide

Available from the Delphi Menu Or via the ‘Manual’ web page

Help, Main Delphi Help Class completion Tells you how to define a basic class

Page 7: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Create your own – Syntax 2unit helperObjects;

interface

uses classes, graphics;

type TMyPrefs = class(TObject) property Name: String; property Color: TColor;end;

Press Ctrl+Shift+C for Class Completion

And you get:

Page 8: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Create your own – Syntax 2unit helperObjects;

interface

uses classes, graphics;

type TMyPrefs = class(TObject) private FColor: TColor; FName: TStrings; procedure SetColor(const Value: TColor); procedure SetName(const Value: String); published property Name: String read FName write SetName; property Color: TColor read FColor write SetColor;end;. . .

Page 9: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Create your own – Syntax 3. . .implementation

procedure TMyPrefs.SetColor(const Value: TColor);Begin FColor := value;End;

procedure TMyPrefs.SetName(const Value: String);Begin if Fname < > Value then FName := Value; end;

Page 10: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Using a class you’ve created 1 In the unit which will use your class

you must: Add the name of your unit

(helperObjects in my example) to the uses clause in the interface section

Declare a variable of type TMyPrefs Private

preferences: TMyPrefs; Create an object of type TMyPrefs

Page 11: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Using a class you’ve created 2 In the unit which will use your class

you must: Create an object of type TMyPrefs

In form OnCreate add: Preferences.create;

Destroy the object before form closes In form OnDestroy add: Preferences.destroy;

Page 12: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Using a class you’ve created 3 In the unit which will use your class,

to use it you’ll need: To write to the property:

Preferences.Name := form.font.name; Preferences.Color := form.color;

To read from the property: Label1.Caption := Preferences.Name;

Page 13: Delphi Classes – roll your own l Delphi classes u Modify to change visibility of properties l Inherit from others u Add extra specialised behaviour l Create

Using a class with Set / Get Properties with set get functions

property Name: String read FName write SetName

encapsulation We can assign to Name but actually call

SetName Allows us to check value ranges Change values without the user seeing Hides complexity.