Bonjour,
Je peine a trouver comment organiser mes classes.
Mon idees et de faire une class par fichier par désir de clarté et avant tout de pouvoir faire de la POO.
J'ai trouver l'exemple suivant sur le net qui montre enfin une classe avec une utilisation d'objet:
Ca marche parfait et enfin un exemple complet.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 // Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses **SysUtils, Forms, Dialogs; type **// Define the classes in this Unit at the very start for clarity **TForm1 = Class;**********// This is a forward class definition **TFruit = Class(TObject)**// This is an actual class definition : ****// Internal class field definitions - only accessible in this unit ****private ******isRound**: Boolean; ******length** : single; ******width****: single; ******diameter : single; ****// Fields and methods only accessible by this class and descendants ****protected ****// Externally accessible fields and methods ****public ******// 2 constructors - one for round fruit, the other long fruit ******constructor Create(diameter : single);************** overload; ******constructor Create(length : single; width : single); overload; ****// Externally accessible and inspectable fields and methods ****published ******// Note that properties must use different names to local defs ******property round : Boolean ********read** isRound; ******property len** : single ********read** length; ******property wide**: single ********read** width; ******property diam**: single ********read** diameter; **end;********************// End of the TFruit class definition **// The actual TForm1 class is now defined **TForm1 = Class(TForm) ****procedure FormCreate(Sender: TObject); ****procedure ShowFruit(fruit : TFruit); **private ****// No local data **public ****// Uses just the TForm ancestor class public definitions **end; var **Form1: TForm1; implementation {$R *.dfm} // Create a round fruit object constructor TFruit.Create(diameter: single); begin **// Indicate that we have a round fruit, and set its size **isRound****** := true; **self.diameter := diameter; end; // Create a long fruit object constructor TFruit.Create(length, width: single); begin **// Indicate that we have a long fruit, and set its size **isRound**** := false; **self.length := length; **self.width**:= width; end; // Form object - action taken when the form is created procedure TForm1.FormCreate(Sender: TObject); var **apple, banana : TFruit; begin **// Let us create our fruit objects **apple**:= TFruit.Create(3.5); **banana := TFruit.Create(7.0, 1.75); **// Show details about our fruits **ShowFruit(apple); **ShowFruit(banana); end; // Show what the characteristics of our fruit are procedure TForm1.ShowFruit(fruit: TFruit); begin **if fruit.round **then ShowMessage('We have a round fruit, with diam = '+ ****************** FloatToStr(fruit.diam)) **else **begin ****ShowMessage('We have a long fruit'); ****ShowMessage('****it has length = '+FloatToStr(fruit.len)); ****ShowMessage('****it has width**= '+FloatToStr(fruit.wide)); **end; end; end. {---------------------------------------------------------------------------------------------------------------------} Sortie: We have a round fruit with diam = 3.5 ** We have a long fruit ** ****it has a length = 7 ** ****it has a width = 1.75
Maintenant je veux avoir deux fichier.
Mon TForm1 dans le premier pour la gestion interface utilisateur et ma classe TFruit dans un deuxieme pour cet objet uniquement.
J'ai fait un premier essai comme cela, voir en suivant, mais j'ai une erreur:
[Erreur fatale] Unit1.pas(7): Fichier non trouvé : 'TFruit.dcu'
Pour memoire TFMain est dans Unit1.pas et TFruit dans Unit2.pas.
Ca dit etre la que cela pèche je suppose.
J'ai aussi mis TFruit dans uses.
Dans le gestionaire de projet les deux unit sont visibles
Source ci_dessous, merci pour vos reponses.
Source unit2:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, TFruit; type TForm1 = class; {------------------------------------------------------------------------------} {------------------------------------------------------------------------------} { TFruit = Class(TObject) // This is an actual class definition : // Internal class field definitions - only accessible in this unit procedure sliceFruit(frName : String); private isRound : Boolean; length : single; width : single; diameter : single; name : String; // Fields and methods only accessible by this class and descendants protected // Externally accessible fields and methods public // 2 constructors - one for round fruit, the other long fruit constructor Create(diameter : single); overload; constructor Create(length : single; width : single); overload; // Externally accessible and inspectable fields and methods published // Note that properties must use different names to local defs property round : Boolean read isRound; property len : single read length; property wide : single read width; property diam : single read diameter; end; } // End of the TFruit class definition {-------------------------------------------------------------------------------} {-------------------------------------------------------------------------------} TForm1 = class(TForm) Button1: TButton; {-----------------------} procedure ShowFruit(fruit : TFruit); procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); {-----------------------} private { Déclarations privées } public { Déclarations publiques } end; var Form1: TForm1; apple, banana : TFruit; implementation {$R *.dfm} {------------------------------------------------------------------------------} {------------------------------------------------------------------------------} { Constructor TFruit.Create(diameter : single); begin isRound := true; self.diameter := diameter; end; Constructor TFruit.Create(length, width : single); begin self.isRound := false; self.length := length; self.width := width; end; } {------------------------------------------------------------------------------} {------------------------------------------------------------------------------} procedure TForm1.ShowFruit(fruit : TFruit); begin ShowMessage('Premier message'); end; {------------------------------------------------------------------------------} procedure TForm1.FormCreate(Sender: TObject); // var apple, banana : TFruit; begin apple := TFruit.Create(3.5); banana := TFruit.Create(7.0, 1.75); ShowFruit(apple); ShowFruit(banana); end; {------------------------------------------------------------------------------} { procedure TFruit.sliceFruit(frName : String); begin ShowMessage('La ' + frName + ' est coupee'); end; } procedure TForm1.Button1Click(Sender: TObject); begin apple.name := 'apple'; // Encapsulation bonjour apple.sliceFruit(apple.name); end; end.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 unit Unit2; interface uses SysUtils, Forms, Dialogs; type TFruit = Class(TObject) // This is an actual class definition : // Internal class field definitions - only accessible in this unit procedure sliceFruit(frName : String); private isRound : Boolean; length : single; width : single; diameter : single; name : String; // Fields and methods only accessible by this class and descendants protected // Externally accessible fields and methods public // 2 constructors - one for round fruit, the other long fruit constructor Create(diameter : single); overload; constructor Create(length : single; width : single); overload; // Externally accessible and inspectable fields and methods published // Note that properties must use different names to local defs property round : Boolean read isRound; property len : single read length; property wide : single read width; property diam : single read diameter; end; // End of the TFruit class definition {-------------------------------------------------------------------------------} {-------------------------------------------------------------------------------} implementation {$R *.dfm} {----------------------------------} {----------------------------------} Constructor TFruit.Create(diameter : single); begin isRound := true; self.diameter := diameter; end; Constructor TFruit.Create(length, width : single); begin self.isRound := false; self.length := length; self.width := width; end; {----------------------------------} {----------------------------------} procedure TFruit.sliceFruit(frName : String); begin ShowMessage('La ' + frName + ' est coupee'); end; end.
Partager