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

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

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

Class Inheritance Demo

Imports System
Imports System.Collections
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Resources
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.IO
Imports System.Drawing.Printing

Module Module1
    Sub Main()
        Using objCar As New SportsCar
            "Set the horsepower and weight(kg)
            objCar.HorsePower = 240
            objCar.Weight = 1085
            "Display the details of the car
            DisplayCarDetails(objCar)
            DisplaySportsCarDetails(objCar)
        End Using
        "Wait for input from the user
        Console.ReadLine()
    End Sub
    "DisplayCarDetails - procedure that displays a car"s details
    Sub DisplayCarDetails(ByVal theCar As Car)
        "Display the details of the car
        Console.WriteLine("Color: " & theCar.Color)
        Console.WriteLine("Number of doors: " & theCar.NumberOfDoors)
        Console.WriteLine("Current speed: " & theCar.Speed)
        Console.WriteLine("Acceleration rate: " & _
            theCar.CalculateAccelerationRate)
    End Sub
    "DisplaySportsCarDetails - procedure that displays a sports car"s details
    Sub DisplaySportsCarDetails(ByVal theCar As SportsCar)
        "Display the details of the sports car
        Console.WriteLine()
        Console.WriteLine("Sports Car Horsepower: " & theCar.HorsePower)
        Console.WriteLine("Sports Car Weight: " & theCar.Weight)
        Console.WriteLine("Power to Weight Ratio: " & theCar.GetPowerToWeightRatio)
    End Sub
End Module
    Public Class Car
        Implements IDisposable
        Public Color As String
        Public HorsePower As Integer
        Private _speed As Integer
        Private _numberOfDoors As Integer
        Public ReadOnly Property Speed() As Integer
            Get
                Return _speed
            End Get
        End Property
        Public Sub Accelerate(ByVal accelerateBy As Integer)
            _speed += accelerateBy
        End Sub
        Public Property NumberOfDoors() As Integer
            Get
                Return _numberOfDoors
            End Get
            Set(ByVal value As Integer)
                If value >= 2 And value <= 5 Then
                    _numberOfDoors = value
                End If
            End Set
        End Property
        Public Function IsMoving() As Boolean
            If Speed = 0 Then
                Return False
            Else
                Return True
            End If
        End Function
        Public Sub New()
            Color = "White"
            _speed = 0
            _numberOfDoors = 5
        End Sub
        Public Overridable Function CalculateAccelerationRate() As Double
            Return 4.2
        End Function
        Private disposed As Boolean = False
        " IDisposable
        Private Overloads Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposed Then
                If disposing Then
                    " TODO: put code to dispose managed resources
                End If
                " TODO: put code to free unmanaged resources here
            End If
            Me.disposed = True
        End Sub
#Region " IDisposable Support "
        " This code added by Visual Basic to correctly implement the disposable pattern.
        Public Overloads Sub Dispose() Implements IDisposable.Dispose
            " Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
        Protected Overrides Sub Finalize()
            " Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(False)
            MyBase.Finalize()
        End Sub
#End Region
    End Class

Public Class SportsCar
    Inherits Car
    Public Weight As Integer
    Public Function GetPowerToWeightRatio() As Double
        Return CType(HorsePower, Double) / CType(Weight, Double)
    End Function
    Public Sub New()
        "Change the default values
        Color = "Green"
        NumberOfDoors = 2
    End Sub
    Public Overrides Function CalculateAccelerationRate() As Double
        "You"ll assume the same 4.2 value, but you"ll multiply it
        "by the power/weight ratio
        Return 4.2 * GetPowerToWeightRatio()
    End Function
End Class


Construct Class by Class Combination

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      Dim circle As Circle
      circle = New Circle(37, 43, 2.5) " instantiate Circle
      Console.WriteLine( "X coordinate is " & circle.X & vbCrLf & _
         "Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _
         circle.Radius )
      circle.X = 2
      circle.Y = 2
      circle.Radius = 4.25
      Console.WriteLine("The new location and radius of circle are " & _
         vbCrLf & circle.ToString() )
      Console.WriteLine( "Diameter is " & _
         String.Format("{0:F}", circle.Diameter()) )
      Console.WriteLine("Circumference is " & _
         String.Format("{0:F}", circle.Circumference()) )
      Console.WriteLine( "Area is " & String.Format("{0:F}", circle.Area()) )
    End Sub
End Class
" Circle class that inherits from class Point.
Public Class Circle
   Inherits Point " Circle Inherits from class Point
   Private mRadius As Double " Circle"s radius
   " default constructor
   Public Sub New()
      " implicit call to Point constructor occurs here
      Radius = 0
   End Sub " New
   " constructor
   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer, ByVal radiusValue As Double)
      " implicit call to Point constructor occurs here
      X = xValue
      Y = yValue
      Radius = radiusValue
   End Sub " New
   " property Radius
   Public Property Radius() As Double
      Get
         Return mRadius
      End Get
      Set(ByVal radiusValue As Double)
         If radiusValue > 0 Then
            mRadius = radiusValue
         End If
      End Set
   End Property " Radius
   " calculate Circle diameter
   Public Function Diameter() As Double
      Return mRadius * 2
   End Function " Diameter
   " calculate Circle circumference
   Public Function Circumference() As Double
      Return Math.PI * Diameter()
   End Function " Circumference
   " calculate Circle area
   Public Function Area() As Double
      Return Math.PI * mRadius ^ 2
   End Function " Area
   " return String representation of Circle
   Public Overrides Function ToString() As String
      Return "Center = " & "[" & X & ", " & Y & "]" & _
         "; Radius = " & mRadius
   End Function " ToString
End Class " Circle
" Point class represents an x-y coordinate pair.
Public Class Point
   " implicitly Inherits Object
   " point coordinate
   Private mX, mY As Integer
   " default constructor
   Public Sub New()
      " implicit call to Object constructor occurs here
      X = 0
      Y = 0
   End Sub " New
   " constructor
   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer)
      " implicit call to Object constructor occurs here
      X = xValue
      Y = yValue
   End Sub " New
   " property X
   Public Property X() As Integer
      Get
         Return mX
      End Get
      Set(ByVal value As Integer)
         mX = value
      End Set
   End Property " X
   " property Y 
   Public Property Y() As Integer
      Get
         Return mY
      End Get
      Set(ByVal value As Integer)
         mY = value
      End Set
   End Property " Y
   " return String representation of Point
   Public Overrides Function ToString() As String
      Return "[" & mX & ", " & mY & "]"
   End Function " ToString
End Class " Point


Four Level Hierachy

Imports System
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
      Dim point As New Point(7, 11)
      Dim circle As New Circle(22, 8, 3.5)
      Dim cylinder As New Cylinder(10, 10, 3.3, 10)
      Dim arrayOfShapes As IShape() = New IShape(2) {}
      arrayOfShapes(0) = point
      arrayOfShapes(1) = circle
      arrayOfShapes(2) = cylinder
      Console.WriteLine( point.Name & ": " & _
         point.ToString() & vbCrLf & circle.Name & ": " & _
         circle.ToString() & vbCrLf & cylinder.Name & _
         ": " & cylinder.ToString() )
      Dim shape As IShape
      For Each shape In arrayOfShapes
         Console.WriteLine(shape.Name & ": " & "Area = " & _
            String.Format("{0:F}", shape.Area()) & _
            "Volume = " & String.Format("{0:F}", shape.Volume()) )
      Next
    End Sub
End Class
Public Interface IShape
   Function Area() As Double
   Function Volume() As Double
   ReadOnly Property Name() As String
End Interface
Public Class Point
   Implements IShape
   Private mX, mY As Integer
   Public Sub New()
      X = 0
      Y = 0
   End Sub
   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer)
      X = xValue
      Y = yValue
   End Sub
   Public Property X() As Integer
      Get
         Return mX
      End Get
      Set(ByVal xValue As Integer)
         mX = xValue
      End Set
   End Property
   Public Property Y() As Integer
      Get
         Return mY
      End Get
      Set(ByVal yValue As Integer)
         mY = yValue
      End Set
   End Property
   Public Overrides Function ToString() As String
      Return "[" & mX & ", " & mY & "]"
   End Function " ToString
   Public Overridable Function Area() As Double _
      Implements IShape.Area
      Return 0
   End Function
   
   Public Overridable Function Volume() As Double _
      Implements IShape.Volume
   Return 0
   End Function
   Public Overridable ReadOnly Property Name() As String _
      Implements IShape.Name
      Get
         Return "Point"
      End Get
   End Property
End Class 

Public Class Circle
   Inherits Point
   Private mRadius As Double
   Public Sub New()
      Radius = 0
   End Sub
   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer, ByVal radiusValue As Double)
      MyBase.New(xValue, yValue)
      Radius = radiusValue
   End Sub
   Public Property Radius() As Double
      Get
         Return mRadius
      End Get
      Set(ByVal radiusValue As Double)
         If radiusValue >= 0 Then
            mRadius = radiusValue
         End If
      End Set
   End Property
   Public Function Diameter() As Double
      Return mRadius * 2
   End Function
   Public Function Circumference() As Double
      Return Math.PI * Diameter()
   End Function
   Public Overrides Function Area() As Double
      Return Math.PI * mRadius ^ 2
   End Function
   Public Overrides ReadOnly Property Name() As String
      Get
         Return "Circle"
      End Get
   End Property
   Public Overrides Function ToString() As String
      Return "Center = " & MyBase.ToString() & _
         "; Radius = " & mRadius
   End Function " ToString
End Class

Public Class Cylinder
   Inherits Circle 
   
   Private mHeight As Double
   Public Sub New()
      Height = 0
   End Sub
   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer, ByVal radiusValue As Double, _
      ByVal heightValue As Double)
      MyBase.New(xValue, yValue, radiusValue)
      Height = heightValue
   End Sub
   Public Property Height() As Double
      Get
         Return mHeight
      End Get
      Set(ByVal heightValue As Double)
         If heightValue >= 0 Then
            mHeight = heightValue
         End If
      End Set
   End Property
   Public Overrides Function Area() As Double
      Return 2 * MyBase.Area + MyBase.Circumference * mHeight
   End Function
   Public Overrides Function Volume() As Double
      Return MyBase.Area * mHeight
   End Function 
   Public Overrides Function ToString() As String
      Return MyBase.ToString() & "; Height = " & mHeight
   End Function " ToString
   Public Overrides ReadOnly Property Name() As String
      Get
         Return "Cylinder"
      End Get
   End Property 
End Class