Diagramme de la classe Tcompte - Billard Club Saint
Transcription
Diagramme de la classe Tcompte - Billard Club Saint
1-8 Exemple d’implémentation et d’utilisation d’une classe sous VB.net Diagramme de la classe Tcompte TCOMPTE - Numéro - nom - solde - telephone + Créer() + Créditer() + Debiter() + Afficher() Ecriture de la classe Tcompte sous VB.net Public Class Tcompte ‘ LES ATTRIBUTS Private sNumero As Integer Private sNom As String Private sSolde As Single Private sTelephone As String ‘ LES ACCESSEURS Public ReadOnly Property Numero() As Integer Get Numero = sNumero End Get End Property Public ReadOnly Property Nom() As String Get Nom = sNom End Get End Property Public ReadOnly Property Solde() As Single Get Solde = sSolde End Get End Property Public Property Telephone() As String Get Telephone = sTelephone End Get Set(ByVal Value As String) sTelephone = Value End Set End Property ‘ LE CONSTRUCTEUR Public Sub New(ByVal UnNumero As Integer, ByVal UnNom As String, ByVal UnTelephone As String) sNumero = UnNumero sNom = UnNom Telephone = UnTelephone sSolde = 0 End Sub ‘LES METHODES Public Sub Crediter(ByVal UnMontant As Integer) sSolde = sSolde + UnMontant End Sub Public Sub Debiter(ByVal UnMontant As Integer) sSolde = sSolde - UnMontant End Sub Public Sub Afficher() Dim msg, titre As String Dim style As Integer msg = Str(Numero) + " " + Nom + " : " + Str(Solde) + " Telephone style = MsgBoxStyle.OKOnly titre = "Gestion de compte" MsgBox(msg, style, titre) End Sub End Class tél : " + Exemple d’utilisation d’objets de la classe Tcompte dans un projet VB.net Code associé à la classe Form1 Public Class Form1 Inherits System.Windows.Forms.Form #Region " Code généré par le Concepteur Windows Form " Public Sub New() MyBase.New() 'Cet appel est requis par le Concepteur Windows Form. InitializeComponent() 'Ajoutez une initialisation quelconque après l'appel InitializeComponent() End Sub 'La méthode substituée Dispose du formulaire pour nettoyer la liste des composants. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Requis par le Concepteur Windows Form Private components As System.ComponentModel.IContainer 'REMARQUE : la procédure suivante est requise par le Concepteur Windows Form 'Elle peut être modifiée en utilisant le Concepteur Windows Form. 'Ne la modifiez pas en utilisant l'éditeur de code. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button5 As System.Windows.Forms.Button Friend WithEvents Button6 As System.Windows.Forms.Button Friend WithEvents Button7 As System.Windows.Forms.Button Friend WithEvents Button8 As System.Windows.Forms.Button Friend WithEvents TextBox2 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Button4 = New System.Windows.Forms.Button Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button5 = New System.Windows.Forms.Button Me.Button6 = New System.Windows.Forms.Button Me.Button7 = New System.Windows.Forms.Button Me.Button8 = New System.Windows.Forms.Button Me.TextBox2 = New System.Windows.Forms.TextBox Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(16, 32) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Créer" ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(16, 64) Me.Button2.Name = "Button2" Me.Button2.TabIndex = 1 Me.Button2.Text = "Créditer" ' 'Button3 ' Me.Button3.Location = New System.Drawing.Point(16, 96) Me.Button3.Name = "Button3" Me.Button3.TabIndex = 2 Me.Button3.Text = "Débiter" ' 'Button4 ' Me.Button4.Location = New System.Drawing.Point(16, 128) Me.Button4.Name = "Button4" Me.Button4.TabIndex = 3 Me.Button4.Text = "Afficher" ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(136, 64) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(88, 20) Me.TextBox1.TabIndex = 4 Me.TextBox1.Text = "" ' 'Button5 ' Me.Button5.Location = New System.Drawing.Point(240, 72) Me.Button5.Name = "Button5" Me.Button5.Size = New System.Drawing.Size(32, 24) Me.Button5.TabIndex = 5 Me.Button5.Text = "Ok" ' 'Button6 ' Me.Button6.Location = New System.Drawing.Point(248, 72) Me.Button6.Name = "Button6" Me.Button6.Size = New System.Drawing.Size(32, 23) Me.Button6.TabIndex = 6 Me.Button6.Text = "Ok" ' 'Button7 ' Me.Button7.Location = New System.Drawing.Point(16, 160) Me.Button7.Name = "Button7" Me.Button7.TabIndex = 7 Me.Button7.Text = "Modifier" ' 'Button8 ' Me.Button8.Location = New System.Drawing.Point(240, 72) Me.Button8.Name = "Button8" Me.Button8.Size = New System.Drawing.Size(40, 23) Me.Button8.TabIndex = 8 Me.Button8.Text = "Ok" ' 'TextBox2 ' Me.TextBox2.Location = New System.Drawing.Point(136, 104) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(88, 20) Me.TextBox2.TabIndex = 9 Me.TextBox2.Text = "" Me.TextBox2.Visible = False ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 230) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.Button8) Me.Controls.Add(Me.Button7) Me.Controls.Add(Me.Button6) Me.Controls.Add(Me.Button5) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Button4) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Gestion de compte" Me.ResumeLayout(False) End Sub #End Region 'Déclaration d' un objet compte Dim c1 As Tcompte ' Créer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click c1 = New Tcompte(1, "Karting", "04 22 11 77 00") End Sub 'Créditer Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Text = "" TextBox1.Visible = True Button5.Visible = True TextBox1.Focus() End Sub 'Ok créditer Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click c1.Crediter(Val(TextBox1.Text)) TextBox1.Visible = False Button5.Visible = False End Sub 'afficher Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click c1.Afficher() End Sub 'Débiter Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click TextBox1.Text = "" TextBox1.Visible = True Button6.Visible = True TextBox1.Focus() End Sub 'Ok débiter Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click c1.Debiter(Val(TextBox1.Text)) TextBox1.Visible = False Button6.Visible = False End Sub 'activation de la fiche Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated TextBox1.Visible = False TextBox2.Visible = False Button5.Visible = False Button6.Visible = False Button8.Visible = False End Sub ' modifier Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click TextBox1.Text = c1.Telephone TextBox1.Visible = True TextBox2.Visible = True TextBox2.Focus() Button8.Visible = True End Sub ' Ok modifier Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click If textbox2.text <> "" Then c1.Telephone = textbox2.text TextBox1.Visible = False TextBox2.Visible = False TextBox2.Text = "" Button8.Visible = False Else : MsgBox(" le champ téléphone est vide ") End If End Sub End Class Destruction des objets sous VB.net : Le garbage collector (également appelé ramasse-miettes) du .NET Framework manage l'allocation et la libération de mémoire dans votre application. Chaque fois que vous utilisez l'opérateur new pour créer un objet, le runtime alloue de la mémoire pour l'objet à partir du tas managé. Tant que de l'espace d'adressage est disponible dans le tas managé, le runtime continue à allouer de l'espace pour les nouveaux objets. Toutefois, la mémoire n'est pas infinie. Le garbage collector doit finir par effectuer un garbage collection afin de libérer de la mémoire. Le moteur d'optimisation du garbage collector détermine le meilleur moment pour effectuer un garbage collection en fonction des allocations en cours. Lorsque le garbage collector effectue un garbage collection, il recherche les objets figurant dans le tas managé qui ne sont plus utilisés par l'application et effectue les opérations nécessaires pour récupérer leur mémoire.