CSDN博客

img colayungnew

接口的应用

发表于2004/10/19 18:53:00  643人阅读

分类: 学习笔记

使用接口:

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

阅读全文
0 0

相关文章推荐

img
取 消
img