' compile with: vbc PolyExampleVB.vb
Class BaseClass
Dim i As Integer = 5
Public Overridable Sub Print()
Console.WriteLine("i is {0}", i)
End Sub
Public Overrides Function ToString() As String
Return i.ToString()
End Function
End Class
Class DerivedClass
Inherits BaseClass
Dim d As Double = 2.1
Public Overrides Sub Print()
MyBase.Print()
Console.WriteLine("d is {0}", d)
End Sub
Public Overrides Function ToString() As String
Return MyBase.ToString() + " " + d.ToString()
End Function
End Class
Class TestBaseDerived
Public Shared Sub Main()
Dim b As New BaseClass()
Dim d As New DerivedClass()
Dim bd As BaseClass = New DerivedClass() ' note this!
b.Print()
d.Print()
bd.Print()
Console.WriteLine("b: {0}, d: {1}, bd: {2}", b, d, bd)
Console.ReadLine()
End Sub
End Class
|