VB.Net/Class/Abstract Class

Материал из VB Эксперт
Перейти к: навигация, поиск

Abstract (MustInherit) Class

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main()
      Dim winArray(3) As Window
      winArray(0) = New ListBox(1, 2, "First List Box")
      winArray(1) = New ListBox(3, 4, "Second List Box")
      winArray(2) = New Button(5, 6)
      Dim i As Integer
      For i = 0 To 2
         winArray(i).DrawWindow(  )
      Next i
   End Sub

End Class

MustInherit Public Class Window
   Public Sub New(top As Integer, left As Integer)
      Me.top = top
      Me.left = left
   End Sub "New
   Public MustOverride Sub DrawWindow(  )
   Protected top As Integer
   Protected left As Integer
End Class "Window
Public Class ListBox
   Inherits Window
   Public Sub New(top As Integer, left As Integer, contents As String)
      MyBase.New(top, left) " call base constructor
      listBoxContents = contents
   End Sub "New
   Public Overrides Sub DrawWindow(  )
      Console.WriteLine("Writing string to the listbox: {0}", listBoxContents)
   End Sub "DrawWindow
   Private listBoxContents As String 
End Class 
Public Class Button
   Inherits Window
   Public Sub New(top As Integer, left As Integer)
      MyBase.New(top, left)
   End Sub 
   Public Overrides Sub DrawWindow(  )
      Console.WriteLine("Drawing a button at {0}, {1}" + ControlChars.Lf, top, left)
   End Sub
End Class 
          
      </source>


Define Abstract Class and Reference Class by it

<source lang="vbnet"> Imports System Public Class MainClass

 Shared Sub Main()
   Dim tom As New Employee("Tom", 50000, "111-11-1234")
   Dim sally As New Employee("Sally", 150000, "111-11-2234")
   Dim ourEmployees(1) As PayableEntity
   ourEmployees(0) = tom
   ourEmployees(1) = sally
   Dim anEmployee As PayableEntity
   For Each anEmployee In ourEmployees
     Console.WriteLine(anEmployee.TheName & " has tax id " & anEmployee.TaxID )
   Next
 End Sub

End Class Public MustInherit Class PayableEntity

 Private m_Name As String
 Public Sub New(ByVal itsName As String)
   m_Name = itsName
 End Sub
 ReadOnly Property TheName() As String
   Get
     Return m_Name
   End Get
 End Property
 Public MustOverride Property TaxID() As String

End Class Public Class Employee

 Inherits PayableEntity
 Private m_Salary As Decimal
 Private m_TaxID As String
 Private Const LIMIT As Decimal = 0.1D
 Public Sub New(ByVal theName As String, ByVal curSalary As Decimal, ByVal TaxID As String)
   MyBase.New(theName)
   m_Salary = curSalary
   m_TaxID = TaxID
 End Sub
 Public Overrides Property TaxID() As String
   Get
     Return m_TaxID
   End Get
   Set(ByVal Value As String)
       m_TaxID = Value
   End Set
 End Property
 ReadOnly Property Salary() As Decimal
   Get
     Return MyClass.m_Salary
   End Get
 End Property
 Public Overridable Overloads Sub RaiseSalary(ByVal Percent As Decimal)
   If Percent > LIMIT Then
     "not allowed
     Console.WriteLine("Percent > LIMIT")
   Else
     m_Salary = (1D + Percent) * m_Salary
   End If
 End Sub
 Public Overridable Overloads Sub RaiseSalary(ByVal Percent As _
 Decimal, ByVal Password As String)
   If Password = "special" Then
     m_Salary = (1D + Percent) * m_Salary
   End If
 End Sub

End Class


      </source>