VB.Net Tutorial/Class Module/Base Class — различия между версиями

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

Текущая версия на 12:55, 26 мая 2010

Call constroctor from base class

Option Strict On
 Imports System
 
 Public Class Rectangle
     Public Sub New(ByVal top As Integer, ByVal left As Integer)
         Me.top = top
         Me.left = left
     End Sub
     Public Sub DrawRectangle( )
         Console.WriteLine("Drawing Rectangle at {0}, {1}", top, left)
     End Sub
     Private top As Integer
     Private left As Integer
 End Class
 Public Class NamedRectangle 
 Inherits Rectangle
     Public Sub New(ByVal top As Integer, ByVal left As Integer, ByVal n As String)
         MyBase.New(top, left)
         RectName = n
     End Sub "New
     Public Shadows Sub DrawRectangle( )
         MyBase.DrawRectangle( ) 
         Console.WriteLine("Writing string to the listbox: {0}", RectName)
     End Sub 
     Private RectName As String
 End Class 
 Module Module1
     Sub Main( )
         Dim w As New Rectangle(5, 10)
         w.DrawRectangle( )
         Dim lb As New NamedRectangle(20, 30, "Hello")
         lb.DrawRectangle( )
     End Sub
 End Module
Drawing Rectangle at 5, 10
Drawing Rectangle at 20, 30
Writing string to the listbox: Hello

Call method with MyBase

Option Strict On
 Imports System
 Public Class Control
     Public Sub New(ByVal top As Integer, ByVal left As Integer)
         Me.top = top
         Me.left = left
     End Sub
     Public Overridable Sub DrawControl( )
         Console.WriteLine("Control: drawing Control at {0}, {1}", top, left)
     End Sub
     Protected top As Integer
     Protected left As Integer
 End Class
 Public Class Label
     Inherits Control
     Public Sub New(ByVal top As Integer, ByVal left As Integer, ByVal n As String)
         MyBase.New(top, left)
         text = n
     End Sub 
     Public Overrides Sub DrawControl( )
         MyBase.DrawControl( ) 
         Console.WriteLine("Writing string to the listbox: {0}", text)
     End Sub 
     Private text As String 
 End Class 
 Public Class Button
     Inherits Control
     Public Sub New(ByVal top As Integer, ByVal left As Integer)
         MyBase.New(top, left)
     End Sub "New
     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 win As New Control(1, 2)
         Dim lb As New Label(3, 4, "test")
         Dim b As New Button(5, 6)
         win.DrawControl( )
         lb.DrawControl( )
         b.DrawControl( )
         
         Dim winArray(3) As Control
         winArray(0) = New Control(1, 2)
         winArray(1) = New Label(3, 4, "AAA")
         winArray(2) = New Button(5, 6)
         Dim i As Integer
         For i = 0 To 2
             winArray(i).DrawControl( )
         Next i
     End Sub
 End Class
Control: drawing Control at 1, 2
Control: drawing Control at 3, 4
Writing string to the listbox: test
Drawing a button at 5, 6
Control: drawing Control at 1, 2
Control: drawing Control at 3, 4
Writing string to the listbox: AAA
Drawing a button at 5, 6

Protected base constructor

Public MustInherit Class Product
  Public Price As Single
  Public Location As String = "Unshelved"
  Public IsSpecial As Boolean = False
  Public Type As String = "Not Defined"
  Protected Sub New(T As String, Optional s As Boolean = False)
    If s Then IsSpecial = True 
    Type = T
  End Sub 
End Class
Class MyProduct
  Inherits Product
  Public Title = "Test Title"
  Public Sub New()
    MyBase.New("CD")
  End Sub
  Public Sub New(IsSpecial As Boolean)
    MyBase.New("CD", IsSpecial)
  End Sub
  Public Sub New(T As String, Optional IsSpecial As Boolean = False)
    Me.New(IsSpecial)
    Title = T
  End Sub
End Class
Module Test
  Sub Main()
    Dim C1 = New MyProduct("S")
    Dim C2 = New MyProduct("H", True)
    C1.Price = 9.99
    C2.Price = 12.99
    Console.WriteLine(C1.Title)
    Console.WriteLine(C1.Location)
    Console.WriteLine(C1.Price)
    
    Console.WriteLine(C2.Title)
    Console.WriteLine(C2.Location)
    Console.WriteLine(C2.Price)
  End Sub
End Module
S
Unshelved
9.99
H
Unshelved
12.99

Shadows method in base class

Option Strict On
 Imports System
 
 Public Class Rectangle
     Public Sub New(ByVal top As Integer, ByVal left As Integer)
         Me.top = top
         Me.left = left
     End Sub
     Public Sub DrawRectangle( )
         Console.WriteLine("Drawing Rectangle at {0}, {1}", top, left)
     End Sub
     Private top As Integer
     Private left As Integer
 End Class
 Public Class NamedRectangle 
 Inherits Rectangle
     Public Sub New(ByVal top As Integer, ByVal left As Integer, ByVal n As String)
         MyBase.New(top, left)
         RectName = n
     End Sub "New
     Public Shadows Sub DrawRectangle( )
         MyBase.DrawRectangle( ) 
         Console.WriteLine("Writing string to the listbox: {0}", RectName)
     End Sub 
     Private RectName As String
 End Class 
 Module Module1
     Sub Main( )
         Dim w As New Rectangle(5, 10)
         w.DrawRectangle( )
         Dim lb As New NamedRectangle(20, 30, "Hello")
         lb.DrawRectangle( )
     End Sub
 End Module
Drawing Rectangle at 5, 10
Drawing Rectangle at 20, 30
Writing string to the listbox: Hello