温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何进行VB.NET继承实现多态应用

发布时间:2021-10-27 17:20:40 来源:亿速云 阅读:174 作者:柒染 栏目:编程语言

这篇文章将为大家详细讲解有关如何进行VB.NET继承实现多态应用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

最为一款面向对象的编程语言,VB.NET同样也可以通过继承进行多态的实现。我们今天就为大家介绍一下有关VB.NET继承实现多态的具体代码编写,希望能给大家带来一些帮助,提高编程效率。

大部分面向对象的程序开发系统都是通过继承来实现多态。比如说跳蚤类和狗类都是从动物类继承过来的。为了突出每一种动物走动的特点,则每一种特定动物类都要重载动物类的"Move"方法。

VB.NET继承实现多态的问题是因为用户可以需要在还不知道是要对哪种特定动物进行处理的时候,就要调用多种从动物类中派生出来的特定的动物类中的"Move"方法。

在下面的这个TestPolymorphism过程中,VB.NET继承实现多态的代码示例:

  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 

关于如何进行VB.NET继承实现多态应用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI