Implementación del Polimorfismo

Fecha: de publicación (16/02/2005)
Autor: Marcos Donalisio (marcos_donalisio@hotmail.com)

 


Esta Implementación se basa en la creación de cuatro clases (Ametralladora, Canon, Arma, Revolver) las cuales las clases Ametralladora, Canon, y revolver heredan de Arma, lo cual tienen que redefinir dos métodos (Cargar y Disparar) que van a hacer distintas cosas dependiendo de la Clase en que se encuentren.

Clase Arma:

Public MustInherit Class Arma

   Private CantidadDeBalas As Integer

   Public Sub New(ByVal CantidadDeBalas As Integer)
      Me.CantidadDeBalas = CantidadDeBalas
   End Sub

   Public MustOverride Sub Disparar()
   Public MustOverride Sub Cargar()
   'metodos que se van a redefinir en las Clases Derivadas 

   Public ReadOnly Property getCantidadBalas() As Integer 'Obtiene la Cantidad de Balas
      Get
         Return Me.CantidadDeBalas
      End Get
   End Property


   Public WriteOnly Property setCantidadBalas() As Integer 'Estable la Cantidad de Balas
      Set(ByVal Value As Integer)
         Me.CantidadDeBalas = Value
      End Set
   End Property

End Class

Clase Ametralladora:

Public Class Ametralladora
   Inherits Arma

   Public Sub New()
      MyBase.New(100)
   End Sub

   Public Overrides Sub Cargar()
      Me.setCantidadBalas = 100
   End Sub

   Public Overrides Sub Disparar()
      If Me.getCantidadBalas >= 10 Then
         Me.setCantidadBalas = Me.getCantidadBalas - 10
      End If
   End Sub
End Class


Clase Canon:

Public Class Canon
   Inherits Arma

   Public Sub New()
      MyBase.New(25)
   End Sub

   Public Overrides Sub Cargar()
      Me.setCantidadBalas = 25
   End Sub

   Public Overrides Sub Disparar()
      If Me.getCantidadBalas >= 5 Then
         Me.setCantidadBalas = Me.getCantidadBalas - 5
      End If
   End Sub
End Class

Clase Revolver:

Public Class Revolver
   Inherits Arma

   Public Sub New()
      MyBase.New(15) 'Se Llama al Constructor de la Clase Base
   End Sub

   Public Overrides Sub Disparar() 'Metodo Redefinido
      If Me.getCantidadBalas >= 1 Then
         Me.setCantidadBalas = Me.getCantidadBalas - 1
      End If
   End Sub

   Public Overrides Sub Cargar() 'Metodo Redefinido
      Me.setCantidadBalas = 15
   End Sub

End Class


Clase Polimorfismo (Formulario):

Public Class Polimorfismo
   Inherits System.Windows.Forms.Form

#Region " Código generado por el Diseñador de Windows Forms "

   Public Sub New()
      MyBase.New()

      'El Diseñador de Windows Forms requiere esta llamada.
      InitializeComponent()

      'Agregar cualquier inicialización después de la llamada a InitializeComponent()
      MyArma(0) = New Revolver
      MyArma(1) = New Ametralladora
      MyArma(2) = New Canon
   End Sub

   'Form reemplaza a Dispose para limpiar la lista de componentes.
   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

   'Requerido por el Diseñador de Windows Forms
   Private components As System.ComponentModel.IContainer

   'NOTA: el Diseñador de Windows Forms requiere el siguiente procedimiento
   'Puede modificarse utilizando el Diseñador de Windows Forms. 
   'No lo modifique con el editor de código.
   Friend WithEvents lblRevolver As System.Windows.Forms.Label
   Friend WithEvents btnCargar As System.Windows.Forms.Button
   Friend WithEvents btnDisparar As System.Windows.Forms.Button
   Friend WithEvents lblAmetralladora As System.Windows.Forms.Label
   Friend WithEvents lblCañon As System.Windows.Forms.Label
   Friend WithEvents pbCanon As System.Windows.Forms.ProgressBar
   Friend WithEvents pbAmetralladora As System.Windows.Forms.ProgressBar
   Friend WithEvents pbRevolver As System.Windows.Forms.ProgressBar
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.btnCargar = New System.Windows.Forms.Button
      Me.btnDisparar = New System.Windows.Forms.Button
      Me.pbCanon = New System.Windows.Forms.ProgressBar
      Me.pbAmetralladora = New System.Windows.Forms.ProgressBar
      Me.pbRevolver = New System.Windows.Forms.ProgressBar
      Me.lblRevolver = New System.Windows.Forms.Label
      Me.lblAmetralladora = New System.Windows.Forms.Label
      Me.lblCañon = New System.Windows.Forms.Label
      Me.SuspendLayout()
      '
      'btnCargar
      '
      Me.btnCargar.FlatStyle = System.Windows.Forms.FlatStyle.System
      Me.btnCargar.Location = New System.Drawing.Point(456, 16)
      Me.btnCargar.Name = "btnCargar"
      Me.btnCargar.TabIndex = 0
      Me.btnCargar.Text = "Cargar"
      '
      'btnDisparar
      '
      Me.btnDisparar.FlatStyle = System.Windows.Forms.FlatStyle.System
      Me.btnDisparar.Location = New System.Drawing.Point(360, 16)
      Me.btnDisparar.Name = "btnDisparar"
      Me.btnDisparar.TabIndex = 1
      Me.btnDisparar.Text = "Disparar"
      '
      'pbCanon
      '
      Me.pbCanon.Location = New System.Drawing.Point(120, 112)
      Me.pbCanon.Name = "pbCanon"
      Me.pbCanon.Size = New System.Drawing.Size(408, 23)
      Me.pbCanon.TabIndex = 2
      '
      'pbAmetralladora
      '
      Me.pbAmetralladora.Location = New System.Drawing.Point(120, 80)
      Me.pbAmetralladora.Name = "pbAmetralladora"
      Me.pbAmetralladora.Size = New System.Drawing.Size(408, 23)
      Me.pbAmetralladora.TabIndex = 3
      '
      'pbRevolver
      '
      Me.pbRevolver.Location = New System.Drawing.Point(120, 48)
      Me.pbRevolver.Name = "pbRevolver"
      Me.pbRevolver.Size = New System.Drawing.Size(408, 23)
      Me.pbRevolver.TabIndex = 4
      '
      'lblRevolver
      '
      Me.lblRevolver.Location = New System.Drawing.Point(8, 48)
      Me.lblRevolver.Name = "lblRevolver"
      Me.lblRevolver.TabIndex = 5
      Me.lblRevolver.Text = "Revolver"
      '
      'lblAmetralladora
      '
      Me.lblAmetralladora.Location = New System.Drawing.Point(8, 80)
      Me.lblAmetralladora.Name = "lblAmetralladora"
      Me.lblAmetralladora.TabIndex = 6
      Me.lblAmetralladora.Text = "Ametralladora"
      '
      'lblCañon
      '
      Me.lblCañon.Location = New System.Drawing.Point(8, 112)
      Me.lblCañon.Name = "lblCañon"
      Me.lblCañon.TabIndex = 7
      Me.lblCañon.Text = "Cañon"
      '
      'Polimorfismo
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(536, 142)
      Me.Controls.Add(Me.lblCañon)
      Me.Controls.Add(Me.lblAmetralladora)
      Me.Controls.Add(Me.lblRevolver)
      Me.Controls.Add(Me.pbRevolver)
      Me.Controls.Add(Me.pbAmetralladora)
      Me.Controls.Add(Me.pbCanon)
      Me.Controls.Add(Me.btnDisparar)
      Me.Controls.Add(Me.btnCargar)
      Me.Name = "Polimorfismo"
      Me.Text = "Polimorfismo"
      Me.ResumeLayout(False)

   End Sub

#End Region

   Dim MyArma(2) As Arma

   <STAThread()> Public Shared Sub main()
      Application.EnableVisualStyles()
      Application.Run(New Polimorfismo)
   End Sub
   Private Sub Polimorfismo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Me.pbRevolver.Maximum = MyArma(0).getCantidadBalas
      Me.pbRevolver.Value = MyArma(0).getCantidadBalas
      Me.pbRevolver.Minimum = 0

      Me.pbAmetralladora.Maximum = MyArma(1).getCantidadBalas
      Me.pbAmetralladora.Value = MyArma(1).getCantidadBalas
      Me.pbAmetralladora.Minimum = 0

      Me.pbCanon.Maximum = MyArma(2).getCantidadBalas
      Me.pbCanon.Value = MyArma(2).getCantidadBalas
      Me.pbCanon.Minimum = 0
   End Sub

   Private Sub btnDisparar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisparar.Click
      MyArma(0).Disparar()
      MyArma(1).Disparar()
      MyArma(2).Disparar()
      ActualizarBarras()
   End Sub

   Private Sub btnCargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargar.Click
      MyArma(0).Cargar()
      MyArma(1).Cargar()
      MyArma(2).Cargar()
      ActualizarBarras()
   End Sub

   Private Sub ActualizarBarras()
      Me.pbRevolver.Value = MyArma(0).getCantidadBalas

      Me.pbAmetralladora.Value = MyArma(1).getCantidadBalas

      Me.pbCanon.Value = MyArma(2).getCantidadBalas
   End Sub
End Class

El Formulario cuenta con dos botones (btnDisparar u btnCargar), tres ProgressBar (pbAmetralladora, pbRevolver y pbCanon), y tre Labels (lblAmetralladora, lblCanon, lblRevolver)


Espacios de nombres usados en el código de este artículo:

 

 


ir al índice

Fichero con el código de ejemplo: Marcos_Implementacion_del_Polimorfismo.zip - 11 KB