最近一直在加班,现在工作的效率是越来越低,现在也没有时间来这里转了。
“委托”这是在.Net中提出一个概念。这个概念是只有在最近使用VB.Net时才刚刚接触(现在的概念太多了,一天一个,追不上了,就懒得追了,等待着剩下的那个了!:<),现在觉得这个东东还挺好用,先写一些现在的使用心得。
我在这个项目之前一直对VB没有什么好感,这个印象是在VB 6.0时期留下的,一直到现在。微软在推出.Net后,将原来的VB进行了大面积的改动,向其中增加了很多新的概念,使它成为了一个真正意义上的面向对象的语言了。在VB.Net中包含了这些语言之后,它的表达能力大大提高,但是它的运行效率还没有提高。
前面说了很多的费话,现在进入我们今天的正题——“委托”。“委托是一种数据结构,它引用静态方法或引用类实例及该类的实例方法。”这个是MSDN中关于委托的定义。这说明在.net中引入“委托”概念的目的,它的概念与C++中的函数指针一致,但在使用上,“委托”要更加灵活。下面是MSDN中关于委托的一段例程。
Public
Class SamplesDelegate
Class SamplesDelegate
' Declares a delegate for a method that takes in an int and returns a String.
Delegate Function myMethodDelegate()Function myMethodDelegate(myInt As Integer) As [String]
' Defines some methods to which the delegate can point.
Public Class mySampleClassClass mySampleClass
' Defines an instance method.
Public Function myStringMethod()Function myStringMethod(myInt As Integer) As [String]
If myInt > 0 Then
Return "positive"
End If
If myInt < 0 Then
Return "negative"
End If
Return "zero"
End Function 'myStringMethod
' Defines a static method.
Public Shared Function mySignMethod()Function mySignMethod(myInt As Integer) As [String]
If myInt > 0 Then
Return "+"
End If
If myInt < 0 Then
Return "-"
End If
Return ""
End Function 'mySignMethod
End Class 'mySampleClass
Public Shared Sub Main()Sub Main()
' Creates one delegate for each method.
Dim mySC As New mySampleClass()
Dim myD1 As New myMethodDelegate(AddressOf mySC.myStringMethod)
Dim myD2 As New myMethodDelegate(AddressOf mySampleClass.mySignMethod)
' Invokes the delegates.
Console.WriteLine("{0} is {1}; use the sign ""{2}"".", 5, myD1(5), myD2(5))
Console.WriteLine("{0} is {1}; use the sign ""{2}"".", - 3, myD1(- 3), myD2(- 3))
Console.WriteLine("{0} is {1}; use the sign ""{2}"".", 0, myD1(0), myD2(0))
End Sub 'Main
End Class ' SamplesDelegate
' Declares a delegate for a method that takes in an int and returns a String.
Delegate Function myMethodDelegate()Function myMethodDelegate(myInt As Integer) As [String]
' Defines some methods to which the delegate can point.
Public Class mySampleClassClass mySampleClass
' Defines an instance method.
Public Function myStringMethod()Function myStringMethod(myInt As Integer) As [String]
If myInt > 0 Then
Return "positive"
End If
If myInt < 0 Then
Return "negative"
End If
Return "zero"
End Function 'myStringMethod
' Defines a static method.
Public Shared Function mySignMethod()Function mySignMethod(myInt As Integer) As [String]
If myInt > 0 Then
Return "+"
End If
If myInt < 0 Then
Return "-"
End If
Return ""
End Function 'mySignMethod
End Class 'mySampleClass
Public Shared Sub Main()Sub Main()
' Creates one delegate for each method.
Dim mySC As New mySampleClass()
Dim myD1 As New myMethodDelegate(AddressOf mySC.myStringMethod)
Dim myD2 As New myMethodDelegate(AddressOf mySampleClass.mySignMethod)
' Invokes the delegates.
Console.WriteLine("{0} is {1}; use the sign ""{2}"".", 5, myD1(5), myD2(5))
Console.WriteLine("{0} is {1}; use the sign ""{2}"".", - 3, myD1(- 3), myD2(- 3))
Console.WriteLine("{0} is {1}; use the sign ""{2}"".", 0, myD1(0), myD2(0))
End Sub 'Main
End Class ' SamplesDelegate
Delegat Function myMethodDelegate(myInt As Integer) As [String] '定义一个委托
这个委托的参数是一个Integer型。
委托在使用方面和普通函数一样,它还可用异步激发,它的这种调用方式要比C++中函数指针更灵活、更方便。
本文是在同事(师傅)讨论之后的一些心得。