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

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

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

Display order in which base-class and derived-class constructors and finalizers are called

Module Tester
   Sub Main()
      Dim circle1, circle2 As Circle
      circle1 = New Circle(72, 29, 4.5) " instantiate objects
      circle2 = New Circle(5, 5, 10)
      circle1 = Nothing " mark objects for garbage collection
      circle2 = Nothing
      System.GC.Collect() " request garbage collector to execute
   End Sub " Main 
End Module 
Public Class Point
   Private mX, mY As Integer
   Public Sub New()
      Console.WriteLine("Point constructor: {0}", Me)
   End Sub " New
   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer)
      Console.WriteLine("Point constructor: {0}", Me)
   End Sub " New
   Protected Overrides Sub Finalize()
      Console.WriteLine("Point Finalizer: {0}", Me)
      MyBase.Finalize() " call Object finalizer
   End Sub 
   " return String representation of Point
   Public Overrides Function ToString() As String
      Return "[" & mX & ", " & mY & "]"
   End Function " ToString
End Class

Public Class Circle
   Inherits Point 
   Private mRadius As Double
   Public Sub New()
      Console.WriteLine("Circle constructor: {0}", Me)
   End Sub " New
   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer, ByVal radiusValue As Double)
      MyBase.New(xValue, yValue)
      Console.WriteLine("Circle constructor: {0}", Me)
   End Sub " New
   Protected Overrides Sub Finalize()
      Console.WriteLine("Circle Finalizer: {0}", Me)
      MyBase.Finalize() " call Point finalizer
   End Sub " Finalize
   Public Overrides Function ToString() As String
      Return "Center = " & MyBase.ToString() & _
         "; Radius = " & mRadius
   End Function " ToString
End Class
Point constructor: Center = [0, 0]; Radius = 0
Circle constructor: Center = [0, 0]; Radius = 0
Point constructor: Center = [0, 0]; Radius = 0
Circle constructor: Center = [0, 0]; Radius = 0
Circle Finalizer: Center = [0, 0]; Radius = 0
Point Finalizer: Center = [0, 0]; Radius = 0
Circle Finalizer: Center = [0, 0]; Radius = 0
Point Finalizer: Center = [0, 0]; Radius = 0

Override Finalize method

Public Class Numbered
    Private m_Number As Integer
    Public Sub New(ByVal my_number As Integer)
        m_Number = my_number
    End Sub
    " Garbage collection started.
    Protected Overrides Sub Finalize()
        Console.WriteLine("Finalize Number: " & m_Number)
    End Sub
End Class

public class Test
   public Shared Sub Main
        Dim new_numbered As New Numbered(1)
        new_numbered = New Numbered(2)
   End Sub
End class
Finalize Number: 2
Finalize Number: 1