-
vb.net教程之vb.net中用继承实现多态性
vb.net中用继承实现多态性
大部分面向对象的程序开发系统都是通过继承来实现多态。比如说跳蚤类和狗类都是从动物类继承过来的。为了突出每一种动物走动的特点,则每一种特定动物类都要重载动物类的"Move"方法。
多态性的问题是因为用户可以需要在还不知道是要对哪种特定动物进行处理的时候,就要调用多种从动物类中派生出来的特定的动物类中的"Move"方法。
在下面的这个TestPolymorphism过程中,用继承来实现多态性:
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
大部分面向对象的程序开发系统都是通过继承来实现多态。比如说跳蚤类和狗类都是从动物类继承过来的。为了突出每一种动物走动的特点,则每一种特定动物类都要重载动物类的"Move"方法。
多态性的问题是因为用户可以需要在还不知道是要对哪种特定动物进行处理的时候,就要调用多种从动物类中派生出来的特定的动物类中的"Move"方法。
在下面的这个TestPolymorphism过程中,用继承来实现多态性:
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
栏目列表
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比