Four Level Hierachy : Inheritance « Class « VB.Net






Four Level Hierachy

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

           
       








Related examples in the same category

1.Construct Class by Class CombinationConstruct Class by Class Combination
2.Property Shadow during InheritanceProperty Shadow during Inheritance
3.Class Inheritance DemoClass Inheritance Demo