使用接口:
Sub TestInterface()
Dim RectangleObject2 As New RectangleClass2()
Dim RightTriangleObject2 As New RightTriangleClass2()
ProcessShape2(RightTriangleObject2, 3, 14)
ProcessShape2(RectangleObject2, 3, 5)
End Sub
Sub ProcessShape2(ByVal Shape2 As Shape2, ByVal X As Double, ByVal Y As Double)
MessageBox.Show("The area of the object is " & Shape2.CalculateArea(X, Y))
End Sub
'使用了接口,就可以通过同一个方法,处理实现了同一接口的不同类
Public Interface Shape2
Function CalculateArea(ByVal X As Double, ByVal Y As Double) As Double
End Interface
Public Class RightTriangleClass2
Implements Shape2
Function CalculateArea(ByVal X As Double, ByVal Y As Double) As Double Implements Shape2.CalculateArea
' Calculate the area of a right triangle.
Return 0.5 * (X * Y)
End Function
End Class
Public Class RectangleClass2
Implements Shape2
Function CalculateArea(ByVal X As Double, ByVal Y As Double) As Double Implements Shape2.CalculateArea
' Calculate the area of a rectangle.
Return X * Y
End Function
End Class
本文地址:http://com.8s8s.com/it/it34887.htm