01-10-12, 07:47 PM
كاتب الموضوع : samerselo
Using Generic Interface
يمكن للواجهات Interfaces أن تكون Generics بحيث يمكنها تزويد نوع واحد أو أكثر من المحددات التي تملأ بنوع المحدد عندما يتم استخدام الواجهة انظر للمثال التالي
فالواجهة IGeneric(Of T) في المثال السابق تستخدم محدد النوع T في كلا الواجهة والطريقة و الفئة Class A تحقق الواجهة IGeneric مستخدما النوع Integer وبالتالي يصبح محدد الوظيفة SomeMethod من النوع Integer وفي الفئة Class B يحققها من النوع Double وبوجود الموجه Option Strict على الوضع Off يسمح بتحويل تضييق ليحول القيمة من النوع Double إلى النوع Integer
Using a Generic Method in an Interface
ليس ضروريا أن تكون الواجهات Generic حتى يكون لها عناصر Generic انظر المثال التالي
الواجهة INonGeneric في المثال السابق ليست Generic ولكن الطريقة SomeMethod(Of T) هي كذلك وعكس المثال السابق فالطريقة ليست محدود بنوع محدد من أجل فئة معينة تحقق تلك الواجهة وهذا المثال يستخدم الموجه Option Strict على الوضع On ليحد من تحويلات التضييق ويثبت أن الأنواع المختلفة مقبول كمحددات
Using Generic Interface
يمكن للواجهات Interfaces أن تكون Generics بحيث يمكنها تزويد نوع واحد أو أكثر من المحددات التي تملأ بنوع المحدد عندما يتم استخدام الواجهة انظر للمثال التالي
كود :
Option Strict Off
Interface IGeneric(Of T)
Sub SomeMethod(ByVal x As T)
End Interface
Class A
Implements IGeneric(Of Integer)
Public Sub SomeMethod(ByVal x As Integer) _
Implements IGeneric(Of Integer).SomeMethod
Console.WriteLine("A.SomeMethod received " + x.ToString())
End Sub
End Class
Class B
Implements IGeneric(Of Double)
Public Sub SomeMethod(ByVal x As Double) _
Implements IGeneric(Of Double).SomeMethod
Console.WriteLine("B.SomeMethod received " + x.ToString())
End Sub
End Class
Public Class EntryPoint
Shared Sub Main()
Dim ca As IGeneric(Of Integer) = New A()
Dim cb As IGeneric(Of Double) = New B()
ca.SomeMethod(123.456)
cb.SomeMethod(123.456)
End Sub
End Class
Using a Generic Method in an Interface
ليس ضروريا أن تكون الواجهات Generic حتى يكون لها عناصر Generic انظر المثال التالي
كود :
Option Strict On
Interface INonGeneric
Sub SomeMethod(Of T)(ByVal x As T)
End Interface
Class A
Implements INonGeneric
Public Sub SomeMethod(Of T)(ByVal x As T) _
Implements INonGeneric.SomeMethod
Console.WriteLine("A.SomeMethod received " + x.ToString())
End Sub
End Class
Public Class EntryPoint
Shared Sub Main()
Dim ca As INonGeneric = New A()
ca.SomeMethod(123.456)
ca.SomeMethod("123 point 456")
End Sub
End Class