Download presentation
Presentation is loading. Please wait.
Published byFelicity Simmons Modified over 9 years ago
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 your own u Create completely new classes F Customer F Preferences F Connection
2
Delphi Classes - scope l Delphi classes u Private F Visible within the class u Protected F Visible to ‘children’ of the class u Public F Visible to all users of the class u Published F Visible properties to all users of the class
3
Delphi Classes – modify l Classes have certain properties made visible l Every class has a custom version – with no properties visible l Use this to decide which fields or attributes you wish to promote to published properties l Can only make more visible in sub- classes not more private
4
Inherit from others l Inherit from the custom version u TCustomListBox u TCustomComboBox u TCustomADODataSet etc.. l Add extra fields or methods l Decide which fields are to be published l Still have access to all the built in behaviour
5
Create your own classes l Create completely new classes u Descend from appropriate base class F Control F Graphic F List etc., u Or from the most basic - TObject
6
Create your own – Syntax 1 l Help, Object Pascal Language Guide u Available from the Delphi Menu u Or via the ‘Manual’ web page l Help, Main Delphi Help u Class completion u Tells you how to define a basic class
7
Create your own – Syntax 2 unit 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:
8
Create your own – Syntax 2 unit 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;...
9
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;
10
Using a class you’ve created 1 l In the unit which will use your class you must: u Add the name of your unit (helperObjects in my example) to the uses clause in the interface section u Declare a variable of type TMyPrefs F Private preferences: TMyPrefs; u Create an object of type TMyPrefs
11
Using a class you’ve created 2 l In the unit which will use your class you must: u Create an object of type TMyPrefs F In form OnCreate add: F Preferences.create; u Destroy the object before form closes F In form OnDestroy add: F Preferences.destroy;
12
Using a class you’ve created 3 l In the unit which will use your class, to use it you’ll need: u To write to the property: F Preferences.Name := form.font.name; Preferences.Color := form.color ; u To read from the property: F Label1.Caption := Preferences.Name;
13
Using a class with Set / Get l Properties with set get functions property Name: String read FName write SetName u encapsulation F We can assign to Name but actually call SetName F Allows us to check value ranges F Change values without the user seeing F Hides complexity.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.