VB.Net Tutorial/Class Module/MustInherit — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 16:40, 26 мая 2010
Содержание
Inherited MustInherit
Public MustInherit Class Cls1
Public MustOverride Function greet1() As String
End Class
Public MustInherit Class Cls2
Inherits Cls1
Public MustOverride Function greet2() As String
End Class
Public Class Cls3
Inherits Cls2
Public Overrides Function greet1() As String
End Function
Public Overrides Function greet2() As String
End Function
End Class
MustInherit class
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
Polymorphic
Imports System.Collections
public class MustOverrideMustInherit
public Shared Sub Main
Dim ShapeArray As New ArrayList()
ShapeArray.Add(New Cone())
ShapeArray.Add(New Cube())
ShapeArray.Add(New Cylinder())
ShapeArray.Add(New Rectangle())
Dim shape As Shape
For Each shape In ShapeArray
Console.WriteLine(shape.DrawMe())
Next
End Sub
End class
Public MustInherit Class Shape
Public MustOverride Function DrawMe() As String
End Class
Public Class Cone
Inherits Shape
Public Overrides Function DrawMe() As String
Return "Drawing Cone..."
End Function
End Class
Public Class Rectangle
Inherits Shape
Public Overrides Function DrawMe() As String
Return "Drawing Rectangle..."
End Function
End Class
Public Class Oval
Inherits Shape
Public Overrides Function DrawMe() As String
Return "Drawing Oval..."
End Function
End Class
Public Class Cylinder
Inherits Shape
Public Overrides Function DrawMe() As String
Return "Drawing Cylinder..."
End Function
End Class
Public Class Cube
Inherits Shape
Public Overrides Function DrawMe() As String
Return "Drawing Cube..."
End Function
End Class
Drawing Cone... Drawing Cube... Drawing Cylinder... Drawing Rectangle...
Use MustInherit to define abstract class
Public MustInherit Class Printable
Public PageData As String
Public MustOverride Sub ShowPage()
Public Sub New(ByVal Message As String)
PageData = Message
End Sub
End Class
Public Class Word
Inherits Printable
Public Overrides Sub ShowPage()
Console.WriteLine("word: " & PageData)
End Sub
Public Sub New(ByVal Message As String)
MyBase.New(Message)
End Sub
End Class
Public Class Text
Inherits Printable
Public Overrides Sub ShowPage()
Console.WriteLine("Test: " & PageData)
End Sub
Public Sub New(ByVal Message As String)
MyBase.New(Message)
End Sub
End Class
Module Module1
Sub Main()
Dim WebPage As Word = New Word("word page")
Dim MobilePage As Text = New Text("text file")
WebPage.ShowPage()
Console.WriteLine()
MobilePage.ShowPage()
End Sub
End Module
word: word page Test: text file