vb.net中用接口完成多態性。本站提示廣大學習愛好者:(vb.net中用接口完成多態性)文章只能為提供參考,不一定能成為您想要的結果。以下是vb.net中用接口完成多態性正文
用戶可以在vb.net中運用接口來完成多態性的完成。經過運用多接口,用戶可以在不中綴運轉代碼的狀況下,允許運轉多種軟件的零碎組件。
接口象類那樣描繪屬性和辦法,但是它和類不同的是,接口不能提供任何implementation。
為了運用接口來完成多態性,用戶需先樹立一個接口,並且經過其他的幾個類完成該接口。用戶可以用簡直相反的辦法調用其他對象曾經完成的辦法。
上面這個例子就是運用接口的辦法完成多態性:
Namespace PolyNamespace Interface Animal Sub Move(ByRef Distance As Double) Sub Bite(Byval What As Object) End Interface Class Flea Implements animal Public Sub bite(Byval What As Object)Implements animal.bite 'Bite something End Sub Sub Move(ByRef Distance AS Double)Implements animal.move distance=Distance+1 End Sub End Class Class Dog Implements animal Public Sub bite(Byval What As Object)Implements animal.bite 'Bite something End Sub Sub Move(ByRef Distance AS Double)Implements animal.move distance=Distance+100 End sub End Class End Namespace 'add this section to the your form Protected Sub Button1_Click(Byval sender As System.Object,_ Byval e As System.EventArgs) Dim aFlea As New Flea() Dim anobj As Object() Dim aDog As New Dog() GetFood(aflea,anobj) GetFood(aDog,anobj) End Sub Public sub GetFood(Byval Critter As Animal,Byval Food As Object) Dim dblDistance As Double 'Code to calculate distance to food(omitted). Critter.Move(dblDistance) 'Early bound(vtable) Critter.Bite(Food) 'Early bound(vtable) End Sub