VB.Net Tutorial/Class Module/MustOverride

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

MustOverride method

Option Strict On
 Imports System
 
 MustInherit Public Class Control
    Public Sub New(top As Integer, left As Integer)
       Me.top = top
       Me.left = left
    End Sub
    Public MustOverride Sub DrawControl( )
    Protected top As Integer
    Protected left As Integer
 End Class 
 Public Class Label
    Inherits Control
    Public Sub New(top As Integer, left As Integer, contents As String)
       MyBase.New(top, left) 
       listBoxContents = contents
    End Sub
    Public Overrides Sub DrawControl( )
       Console.WriteLine("Writing string to the listbox: {0}", listBoxContents)
    End Sub 
    Private listBoxContents As String 
 End Class
 Public Class Button
    Inherits Control
    Public Sub New(top As Integer, left As Integer)
       MyBase.New(top, left)
    End Sub
    Public Overrides Sub DrawControl( )
       Console.WriteLine("Drawing a button at {0}, {1}" + ControlChars.Lf, top, left)
    End Sub 
 End Class
 Public Class Tester
    Shared Sub Main( )
       Dim winArray(3) As Control
       winArray(0) = New Label(1, 2, "A")
       winArray(1) = New Label(3, 4, "B")
       winArray(2) = New Button(5, 6)
       Dim i As Integer
       For i = 0 To 2
          winArray(i).DrawControl( )
       Next i
    End Sub "Main
 End Class "Tester
Writing string to the listbox: A
Writing string to the listbox: B
Drawing a button at 5, 6

MustOverrides MustInherit

public class MustOverrideMustInherit
   public Shared Sub Main
        Dim Emp1 As New Employee()
        Emp1.Name = "B"
        Console.WriteLine(GetName(Emp1))
        Dim Cus1 As New Customer()
        Cus1.Name = "T"
        Console.WriteLine(GetName(Cus1))
   End Sub
    Public Shared Function GetName(ByVal p As Person) As String
        Return p.Name
    End Function
End class
Public MustInherit Class Person
    Public MustOverride Property Name() As String
End Class
Public Class Employee
    Inherits Person
    Private m_Name
    Public Overrides Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal Value As String)
            m_Name = Value
        End Set
    End Property
End Class

Public Class Customer
    Inherits Person
    Private m_Name
    Public Overrides Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal Value As String)
            m_Name = Value
        End Set
    End Property
End Class
B
T