溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何進行VB.NET繼承實現(xiàn)多態(tài)應用

發(fā)布時間:2021-10-27 17:20:40 來源:億速云 閱讀:184 作者:柒染 欄目:編程語言

這篇文章將為大家詳細講解有關(guān)如何進行VB.NET繼承實現(xiàn)多態(tài)應用,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

最為一款面向?qū)ο蟮木幊陶Z言,VB.NET同樣也可以通過繼承進行多態(tài)的實現(xiàn)。我們今天就為大家介紹一下有關(guān)VB.NET繼承實現(xiàn)多態(tài)的具體代碼編寫,希望能給大家?guī)硪恍椭?,提高編程效率?/p>

大部分面向?qū)ο蟮某绦蜷_發(fā)系統(tǒng)都是通過繼承來實現(xiàn)多態(tài)。比如說跳蚤類和狗類都是從動物類繼承過來的。為了突出每一種動物走動的特點,則每一種特定動物類都要重載動物類的"Move"方法。

VB.NET繼承實現(xiàn)多態(tài)的問題是因為用戶可以需要在還不知道是要對哪種特定動物進行處理的時候,就要調(diào)用多種從動物類中派生出來的特定的動物類中的"Move"方法。

在下面的這個TestPolymorphism過程中,VB.NET繼承實現(xiàn)多態(tài)的代碼示例:

  1. MustInherit Public Class Amimal 
    '基本類  

  2. MustOverride Public Sub Bite
    (Byval What As Object)  

  3. MustOverride Public Sub Move
    (ByRef Distance As Double)  

  4. End Class  

  5. Public Class Flea  

  6. Inherits Amimal  

  7. Overrides Sub bite(Byval What 
    As Object)  

  8. 'Bite something  

  9. End Sub  

  10. Overrides Sub Move(ByRef 
    Distance As Double)  

  11. distance=Distance+1  

  12. End Sub  

  13. End Class  

  14. Public Class Dog  

  15. Inherits Animal  

  16. Overrides Public Sub bite
    (Byval What As Object)  

  17. 'Bite something  

  18. End Sub  

  19. Overrides Sub Move(ByRef 
    Distance As Double)  

  20. distance=Distance+100  

  21. End Sub  

  22. End Class  

  23. Sub TestPolymorphism()  

  24. Dim aDog As New Dog()  

  25. Dim aFlea As New Flea()  

  26. UseAnimal(aFlea) 'Pass a flea 
    object to UseAnimal procedure  

  27. UseAnimal(aDog) 'Pass a Dog 
    object to UseAnimal procedure  

  28. End Sub  

  29. Sub UseAnimal(Byval AnAnimal As Animal)  

  30. Dim distance As Double=0 

  31. 'UseAnimal does not care what 
    kind of animal it is using  

  32. 'The Move method of both the 
    Flea and the Dog are inherited  

  33. 'from the Animal class and can 
    be used interchangeably.  

  34. AnAniml.Move(distance)  

  35. If distance=1 Then  

  36. MessageBox.Show("The animal moved:
    "&CStr(distance)&_  

  37. "units,so it must be a Flea.")  

  38. ElseIf distance>1 Then  

  39. MessageBox.Show("The animal 
    moved:"&CStr(distance)&_  

  40. "units,so it must be a Dog.")  

  41. End IF  

  42. End Sub 

關(guān)于如何進行VB.NET繼承實現(xiàn)多態(tài)應用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI