例子演示下VB实现多态

在工程中创建3个Class Module,名字分别为:Animal、Cat、Dog:

Animal是接口:
Sub Cry()

End Sub

Cat和Dog类是实现:

Implements Animal

Private Sub Animal_Cry()
    MsgBox "miao miao"
End Sub


Implements Animal


Private Sub Animal_Cry()
    MsgBox "wang wang"
End Sub


然后调用的代码如下:
Private Sub Command1_Click()
    Dim a1 As Animal
    Dim a2 As Animal
   
    Set a1 = New Dog
    Set a2 = New Cat
   
    a1.Cry
    a2.Cry
End Sub

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页