-
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
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比