接口的应用

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

使用接口:

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航