agosto 04, 2006

Ejemplo de una clase abstracta

EL SIGUIENTE PROGRAMA ES UNA APLICACION DE WINDOWS.
CONTIENE:
TRES CLASES, LAS CUALES ESTAN DIVIDIDAS EN DOS BLOQUES UNO ACTIVO Y OTRO EN COMENTARIOS. EN EL BLOQUE DE COMENTARIOS SE ESTA IMPLEMENTANDO LAS CLASES ABSTRACTAS, EN EL QUE NO TIENE COMENTARIOS SOLO SE LE ESTA APLICANDO HERENCIA.

Public Class Cuenta
Protected m_balance As Decimal
Public Property balance() As Decimal
Get
Return m_balance
End Get
Set(ByVal Value As Decimal)
m_balance = Value
End Set
End Property


Public Sub New(Optional ByVal iniciobalance As Decimal = 0)
m_balance = iniciobalance
End Sub
End Class
'**********************************************************************
'Public MustInherit Class Cuenta
' Protected m_balance As Decimal
' Public Property balance() As Decimal
' Get
' Return m_balance
' End Get
' Set(ByVal Value As Decimal)
' m_balance = Value
' End Set
' End Property

' Public MustOverride Sub CierredelMes()
' Public Sub New(Optional ByVal iniciobalance As Decimal = 0)
' m_balance = iniciobalance
' End Sub
'End Class


Public Class cuentadeahorro
Inherits Cuenta
'interes compartido, por default es 0.5% al mes
'The Shared keyword indicates that one or more declared programming elements are shared. Shared elements are not associated with a specific instance of a class or structure. You can access them by qualifying them either with the class or structure name, or with the variable name of a specific instance of the class or structure.

Protected Shared s_interes As Decimal = CDec(0.005)

Public Shared Property interes() As Decimal
Get
Return s_interes
End Get
Set(ByVal Value As Decimal)
s_interes = Value
End Set
End Property

Public Sub New(Optional ByVal iniciobalance As Decimal = 0)
MyBase.New(iniciobalance)
End Sub

Public Sub CierredelMes()
If m_balance < 0 Then
m_balance -= 25
Else
m_balance *= 1 + s_interes
End If
End Sub
End Class
'**********************************************************************
'Public Class cuentadeahorro
' Inherits Cuenta
' Protected Shared s_interes As Decimal = CDec(0.005)

' Public Shared Property interes() As Decimal
' Get
' Return s_interes
' End Get
' Set(ByVal Value As Decimal)
' s_interes = Value
' End Set
' End Property

' Public Sub New(Optional ByVal iniciobalance As Decimal = 0)
' MyBase.New(iniciobalance)
' End Sub

' Public Overrides Sub CierredelMes()
' If m_balance < 0 Then
' m_balance -= 25
' Else
' m_balance *= 1 + s_interes
' End If
' End Sub
'End Class

Public Class CuentadeCheques
Inherits Cuenta
Public Sub CierredelMes()
If m_balance < 0 Then
m_balance -= 25
ElseIf m_balance < 2500 Then
m_balance -= 4
End If
End Sub

Public Sub New(Optional ByVal iniciobalance As Decimal = 0)
m_balance = iniciobalance
End Sub
End Class
'**********************************************************************
'Public Class CuentadeCheques
' Inherits Cuenta
' Public Overrides Sub CierredelMes()
' If m_balance < 0 Then
' m_balance -= 25
' ElseIf m_balance < 2500 Then
' m_balance -= 4
' End If
' End Sub

' Public Sub New(Optional ByVal iniciobalance As Decimal = 0)
' m_balance = iniciobalance
' End Sub
'End Class

CODIGO DEL BOTON DE LA FORMA:

Dim cc As CuentadeCheques = New CuentadeCheques(CDec(txtinibalanceCC.Text))
cc.CierredelMes()
txtbalanceCC.Text = cc.balance

Dim ca As cuentadeahorro = New cuentadeahorro(CDec(txtIniBalanceCA.Text))
ca.CierredelMes()
txtbalanceCA.Text = ca.balance
'**********************************************************************
'Dim c As Cuenta = New CuentadeCheques(CDec(txtinibalanceCC.Text))
'c.CierredelMes()
'txtbalanceCC.Text = "$ " & c.balance

'c = New cuentadeahorro(CDec(txtIniBalanceCA.Text))
'c.CierredelMes()
'txtbalanceCA.Text = "$ " & c.balance

No hay comentarios.: