Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Module Module1
- Sub Main()
- ' Create some objects
- Dim dObj() = New DrawingObject(1) {}
- dObj(0) = New Circle(50)
- dObj(1) = New Square(10, 10)
- For Each drawObj As DrawingObject In dObj
- Select Case drawObj.GetType().Name
- Case "Circle"
- Dim circle As Circle = DirectCast(drawObj, Circle)
- Console.WriteLine(String.Format("I'm a {0} with area of {1} derived from {2}", circle.Draw(), circle.Area(), circle.DrawBase()))
- Case "Square"
- Dim square As Square = DirectCast(drawObj, Square)
- Console.WriteLine(String.Format("I'm a {0} with area of {1} derived from {2}", square.Draw(), square.Area(), square.DrawBase()))
- End Select
- Next
- End Sub
- Public MustInherit Class DrawingObject
- ' Area() function must be over ridden in derived classes to provide specific functionality
- Public MustOverride Function Area() As Double
- ' DrawBase() function is available from all derived classes
- Public Function DrawBase() As String
- Return "DrawingObject"
- End Function
- End Class
- Public Class Square
- Inherits DrawingObject
- Public Property Width() As Integer
- Public Property Length() As Integer
- Public Sub New(w As Integer, l As Integer)
- width = w
- length = l
- End Sub
- Public Function Draw() As String
- Return "Square"
- End Function
- Public Overrides Function Area() As Double
- Return Width * Length
- End Function
- End Class
- Public Class Circle
- Inherits DrawingObject
- Public Property Radius() As Integer
- Public Sub New(r As Integer)
- Radius = r
- End Sub
- Public Function Draw() As String
- Return "Circle"
- End Function
- Public Overrides Function Area() As Double
- Return (Math.Pow(Radius, 2) * Math.PI)
- End Function
- End Class
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement