最为一款面向对象的编程语言,VB.NET同样也可以通过继承进行多态的实现。我们今天就为大家介绍一下有关VB.NET继承实现多态的具体代码编写,希望能给大家带来一些帮助,提高编程效率。
大部分面向对象的程序开发系统都是通过继承来实现多态。比如说跳蚤类和狗类都是从动物类继承过来的。为了突出每一种动物走动的特点,则每一种特定动物类都要重载动物类的"Move"方法。
VB.NET继承实现多态的问题是因为用户可以需要在还不知道是要对哪种特定动物进行处理的时候,就要调用多种从动物类中派生出来的特定的动物类中的"Move"方法。
在下面的这个TestPolymorphism过程中,VB.NET继承实现多态的代码示例:
- MustInherit Public Class Amimal
'基本类- MustOverride Public Sub Bite
(Byval What As Object)- MustOverride Public Sub Move
(ByRef Distance As Double)- End Class
- Public Class Flea
- Inherits Amimal
- Overrides Sub bite(Byval What
As Object)- 'Bite something
- End Sub
- Overrides Sub Move(ByRef
Distance As Double)- distance=Distance+1
- End Sub
- End Class
- Public Class Dog
- Inherits Animal
- Overrides Public Sub bite
(Byval What As Object)- 'Bite something
- End Sub
- Overrides Sub Move(ByRef
Distance As Double)- distance=Distance+100
- End Sub
- End Class
- Sub TestPolymorphism()
- Dim aDog As New Dog()
- Dim aFlea As New Flea()
- UseAnimal(aFlea) 'Pass a flea
object to UseAnimal procedure- UseAnimal(aDog) 'Pass a Dog
object to UseAnimal procedure- End Sub
- Sub UseAnimal(Byval AnAnimal As Animal)
- Dim distance As Double=0
- 'UseAnimal does not care what
kind of animal it is using- 'The Move method of both the
Flea and the Dog are inherited- 'from the Animal class and can
be used interchangeably.- AnAniml.Move(distance)
- If distance=1 Then
- MessageBox.Show("The animal moved:
"&CStr(distance)&_- "units,so it must be a Flea.")
- ElseIf distance>1 Then
- MessageBox.Show("The animal
moved:"&CStr(distance)&_- "units,so it must be a Dog.")
- End IF
- End Sub
VB.NET继承实现多态的相关代码编写就为大家介绍到这里。
【编辑推荐】
本文题目:VB.NET继承实现多态应用方法介绍
本文来源:http://www.shufengxianlan.com/qtweb/news4/353304.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联