委托可以认为是一类方法签名一致的方法抽象类。
委托对象必须由签名一致的方法实例化,而通过调用委托对象可以调用实例化委托对象的方法。
- Public Class Form1
- Public Delegate Function addMethod(ByVal x, ByVal y) As Integer
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim a As addMethod
- Dim b As Tdelegate
- b = New Tdelegate()
- a = New addMethod(AddressOf b.add)
- 'TextBox1.Text = TextBox1.Text + a(3, 9).ToString
- 'TextBox1.Text = TextBox1.Text + (New addMethod(AddressOf New Tdelegate().add))(3, 9).ToString
- TextBox1.Text = TextBox1.Text + New addMethod(AddressOf New Tdelegate().add).Invoke(3, 9).ToString
- End Sub
- End Class
- Public Class Tdelegate
- Public Function add(ByVal x, ByVal y) As Integer
- Return x + y
- End Function
- End Class