-
VB.net学习笔记(八)重载与共享
Public Sub Walk() '1、重载,无参数
RaiseEvent Walked(0)
End Sub
Public Sub Walk(ByVal Distance As Integer) '2、重载,带一个参数
mintTotalDistance += Distance
RaiseEvent Walked(Distance)
End Sub
Public Sub Walk(Optional ByVal Distance As Integer = 0) '3、上面1和2简化写成一个
mintTotalDistance += Distance
RaiseEvent Walked(Distance)
End Sub
Public Sub DoWork(Byval x as integer,optional byval y as integer=0 ) '1
Public Sub DoWork(Byval x as integer) '2
Public Sub DoWork(Byval x as String) '3
Public Sub New(Optional ByVal Name As String = "", Optional ByVal BirthDate As Date =#1/1/1900#)
mstrName = Name
mdtBirthDate = BirthDate
Phone("home") = "555-1234"
Phone("work") = "555-5678"
sintCounter += 1
RaiseEvent NewPerson()
End Sub
Public Sub New(ByVal Name As String, ByVal BirthDate As Date)
mstrName = Name
mdtBirthDate = BirthDate
Phone("home") = "555-1234"
Phone("work") = "555-5678"
sintCounter += 1
RaiseEvent NewPerson()
End Sub
Public Class Person
'.......其它代码
Private Shared sintCounter As Integer '共享变量
Public Sub New(ByVal Name As String, ByVal BirthDate As Date)
mstrName = Name
mdtBirthDate = BirthDate
Phone("home") = "555-1234"
Phone("work") = "555-5678"
sintCounter += 1 ’每增加一个对象自动计数
RaiseEvent NewPerson()
End Sub
Public Sub New()
Phone("home") = "555-1234"
Phone("work") = "555-5678"
sintCounter += 1 '自动统计计数
RaiseEvent NewPerson()
End Sub
'......
End Class
Public Shared ReadOnly Property PersonCount() As Integer
Get
Return sintCounter
End Get
End Property
Dim myPerson As Person
myPerson = New Person
myPerson = New Person
myPerson = New Person
MessageBox.Show(Person.PersonCount()) '1、标准写法,共享数据通过类名访问
MessageBox.Show(PersonCount()) '2、错误,未限定范围(不知是哪个类的)
MessageBox.Show(myPerson.PersonCount()) '3、非标准(发出警告)因模糊了实例与类的共享成员(不推荐)
Public Shared ReadOnly Property PersonCount() As Integer '只读属性PersonCount
Get
Return sintCounter
End Get
End Property
Public Shared Function CompareAge(ByVal Person1 As Person, ByVal Person2 As Person) As Boolean
Return Person1.Age > Person2.Age
End Function
共享属性的另一例:
Public Shared ReadOnly Property RetirementAge() As Integer '只读属性RetirementAge
Get
Return 62
End Get
End Property
Public Shared Event NewPerson()
Public Class Person
'.........
Public Shared Event NewPerson()
Public Sub New(ByVal Name As String, ByVal BirthDate As Date)
mstrName = Name
mdtBirthDate = BirthDate
Phone("home") = "555-1234"
Phone("work") = "555-5678"
sintCounter += 1
RaiseEvent NewPerson() '普通方法调用共享事件
End Sub
Public Sub New()
Phone("home") = "555-1234"
Phone("work") = "555-5678"
sintCounter += 1
RaiseEvent NewPerson()
End Sub
'..........
End Class
复习:事件其实就类似于回调函数。
Private Shared sintCounter As Integer
Shared Sub New()
sintCounter = 0
End Sub
三、运算符重载。
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Person = New Person(3)
Dim b As Person = New Person(4)
a = a + 3
TextBox1.Text = a.show()
End Sub
End Class
Public Class Person
Private b As Int32
Public Sub New(Optional d As Int32 = 0)
b = d
End Sub
Public Shared Operator +(ByVal f As Person, ByVal d As Int32) As Person
f.b = f.b + d
Return f
End Operator
Public Shared Operator =(ByVal c As Person, ByVal d As Person) As Boolean
Return c.b = d.b
End Operator
'Public Shared Operator >(ByVal c As Person, ByVal d As Person) As Boolean
' Return c.b > d.b
'End Operator
'Public Shared Operator <(ByVal c As Person, ByVal d As Person) As Boolean
' Return c.b < d.b
'End Operator
Public Shared Operator <>(ByVal c As Person, ByVal d As Person) As Boolean
Return c.b <> d.b
End Operator
Public Function show() As Int32
Return b
End Function
End Class
另外,对于CType运算符的重载,还必须出现Narrowing或者Widening,用来指明取值范围是扩大还是收缩。
Public Shared Narrowing Operator CType(ByVal name As String) As Person
Dim obj As New Person 'Ctype必须带有Narrowing(收缩)或Widening(扩展)来指明类型的变化范围
obj.Name = name
Return obj
End Operator
Public Shared Widening Operator CType(ByVal obj As Person) As String
Return obj.Name 'Person->String转换,由窄转向宽,扩展,故用windening
End Operator
栏目列表
最新更新
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() 对比