'ex01.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'-------------------------------------------------------------------------
Class Shape
Protected lx, ly As Integer
Public Sub SetXY(ByVal x As Integer, ByVal y As Integer)
lx = x
ly = y
End Sub
Public Overridable Sub Draw()
End Sub
Public Overridable Function Clone() As Shape
End Function
End Class
Class Circle
Inherits Shape
Public Overrides Sub Draw()
MessageBox.Show("Drawing a Circle at (" + str(lx) + ", " + str(ly) + ")")
End Sub
Public Overrides Function Clone() As Shape
Clone = New Circle()
End Function
End Class
Class Rectangle
Inherits Shape
Public Overrides Sub Draw()
MessageBox.Show("Drawing a Rectangle at (" + str(lx) + ", " + str(ly) + ")")
End Sub
Public Overrides Function Clone() As Shape
Clone = New Rectangle()
End Function
End Class
'-------------------------------------------------------------------------------------
Class ShapeList
Private tlist(10) As Shape
Private counter As Integer
Public Sub New()
counter = 0
End Sub
Public Sub AddShape(ByVal sobj As Shape)
tlist(counter) = sobj
counter = counter + 1
End Sub
Public Function GetShape(ByVal i As Integer) As Shape
GetShape = tlist(i)
End Function
Public Sub Draw()
Dim i As Integer
For i = 0 To counter - 1
tlist(i).Draw()
Next
End Sub
End Class
'-------------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form
Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'TODO: Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
......
#End Region
Protected Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim list As New ShapeList()
Dim ps As Shape
ps = New Circle()
ps.SetXY(10, 10)
list.AddShape(ps)
ps = New Rectangle()
ps.SetXY(50, 50)
list.AddShape(ps)
Public Overridable Sub Draw()
End Sub
Public Overridable Function Clone() As Shape
End Function
Draw( )和Clone( )皆是虛擬(virtual)程式,以發揮多型(polymorphism)功能。lx及ly是圖形的左上方座標。SetXY( )可改變lx及ly值。ShapeList類別的Draw程式用來繪出串列中的各prototype對象圖。
VB的父系類別(superclass)有兩種角色:
1) 提供一些程式給子類別繼承
2) 作為各子類別的共同介面(Interface)
'ex02.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'----------------------------------------------------
Interface IShape
Sub Draw()
Function Clone() As IShape
Sub SetXY(ByVal x As Integer, ByVal y As Integer)
End Interface
Class Shape
Protected lx, ly As Integer
Protected Sub SetXY(ByVal x As Integer, ByVal y As Integer)
lx = x
ly = y
End Sub
End Class
Class Circle
Implements IShape
Inherits Shape
Public Sub Draw() Implements IShape.Draw
MessageBox.Show("Drawing a Circle at (" + str(lx) + ", " + str(ly) + ")")
End Sub
Public Function Clone() As IShape Implements IShape.Clone
Clone = New Circle()
End Function
Public Sub SetValue(ByVal x As Integer, ByVal y As Integer) Implements IShape.SetXY
MyBase.SetXY(x, y)
End Sub
End Class
Class Rectangle
Inherits Shape
Implements IShape
Public Sub Draw() Implements IShape.Draw
MessageBox.Show("Drawing a Rectangle at (" + str(lx) + ", " + str(ly) + ")")
End Sub
Public Function Clone() As IShape Implements IShape.Clone
Clone = New Rectangle()
End Function
Public Sub SetValue(ByVal x As Integer, ByVal y As Integer) Implements IShape.SetXY
MyBase.SetXY(x, y)
End Sub
End Class
'------------------------------------------------------------------------------------------
Class ShapeList
Private tlist(10) As IShape
Private counter As Integer
Public Sub New()
counter = 0
End Sub
Public Sub AddShape(ByVal sobj As IShape)
tlist(counter) = sobj
counter = counter + 1
End Sub
Public Function GetShape(ByVal i As Integer) As IShape
GetShape = tlist(i)
End Function
Public Sub Draw()
Dim i As Integer
For i = 0 To counter - 1
tlist(i).Draw()
Next
End Sub
End Class
'------------------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form
Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'TODO: Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
......
#End Region
Protected Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim list As New ShapeList()
Dim pShape As IShape
pShape = New Circle()
pShape.SetXY(10, 10)
list.AddShape(pShape)
pShape = New Rectangle()
pShape.SetXY(50, 50)
list.AddShape(pShape)