zoukankan      html  css  js  c++  java
  • 泛型方法和接口

    代码
    Module Functions

       
    Function IsDefaultValue(Of T)(ByVal value As T) As Boolean
          
    Dim defValue As T = Nothing
          
    Return Object.Equals(value, defValue)
       
    End Function

       
    ' Exchange two arguments passed by address.
       Sub Swap(Of T)(ByRef x As T, ByRef y As T)
          
    Dim tmp As T = x
          x 
    = y
          y 
    = tmp
       
    End Sub

       
    Function Max(Of T As IComparable)(ByVal ParamArray values() As T) As T
          
    Dim result As T = values(0)
          
    For i As Integer = 1 To UBound(values)
             
    If result.CompareTo(values(i)) < 0 Then result = values(i)
          
    Next
          
    Return result
       
    End Function

       
    Function MedianValue(Of T As IComparable(Of T))(ByVal list As List(Of T), _
          
    Optional ByVal position As Integer = -1As T
          
    ' Provide a default value for second argument.
          If position < 0 Then position = list.Count \ 2

          
    ' If the list has just one element, we've found its median.
          Dim guess As T = list(0)
          
    If list.Count = 1 Then Return guess
          
    ' These list will contain values lower and higher than the current guess.
          Dim lowerList As New List(Of T)
          
    Dim higherList As New List(Of T)

          
    For i As Integer = 1 To list.Count - 1
             
    Dim value As T = list(i)
             
    If guess.CompareTo(value) <= 0 Then
                
    ' The value is higher than or equal to the current guess.
                higherList.Add(value)
             
    Else
                
    ' The value is lower than the current guess.
                lowerList.Add(value)
             
    End If
          
    Next

          
    If lowerList.Count > position Then
             
    ' The median value must be in the lower-than list.
             Return MedianValue(lowerList, position)
          
    ElseIf lowerList.Count < position Then
             
    ' The median value must be in the higher-than list.
             Return MedianValue(higherList, position - lowerList.Count - 1)
          
    Else
             
    ' The guess is correct.
             Return guess
          
    End If
       
    End Function

       
    Public Function CreateObject(Of T As New)() As T
          
    Return New T
       
    End Function

       
    Public Function CreateArray(Of T As New)(ByVal numEls As IntegerAs T()
          
    Dim values(numEls - 1As T
          
    For i As Integer = 0 To numEls - 1
             values(i) 
    = New T
          
    Next
          
    Return values
       
    End Function



    End Module
    Public Interface ICalculator(Of T)
       
    Function Add(ByVal n1 As T, ByVal n2 As T) As T
       
    Function Subtract(ByVal n1 As T, ByVal n2 As T) As T
       
    Function Multiply(ByVal n1 As T, ByVal n2 As T) As T
       
    Function Divide(ByVal n1 As T, ByVal n2 As T) As T
       
    Function ConvertTo(ByVal n As ObjectAs T
    End Interface
    Interface IAdder(Of T)
       
    Function Add(ByVal n1 As T, ByVal n2 As T) As T
    End Interface


    Public Class NumericCalculator
       
    Implements ICalculator(Of Integer)
       
    Implements ICalculator(Of Double)

       
    ' The ICalculator(Of Integer) interface
       Public Function AddInt32(ByVal n1 As IntegerByVal n2 As IntegerAs Integer _
             
    Implements ICalculator(Of Integer).Add
          
    Return n1 + n2
       
    End Function
       
    Public Function SubtractInt32(ByVal n1 As IntegerByVal n2 As IntegerAs Integer _
             
    Implements ICalculator(Of Integer).Subtract
          
    Return n1 - n2
       
    End Function
       
    Public Function MultiplyInt32(ByVal n1 As IntegerByVal n2 As IntegerAs Integer _
             
    Implements ICalculator(Of Integer).Multiply
          
    Return n1 * n2
       
    End Function
       
    Public Function DivideInt32(ByVal n1 As IntegerByVal n2 As IntegerAs Integer _
             
    Implements ICalculator(Of Integer).Divide
          
    Return n1 \ n2
       
    End Function
       
    Public Function ConvertToInt32(ByVal n As ObjectAs Integer _
             
    Implements ICalculator(Of Integer).ConvertTo
          
    Return CInt(n)
       
    End Function

       
    ' The ICalculator(Of Double) interface
       Public Function AddDouble(ByVal n1 As DoubleByVal n2 As DoubleAs Double _
             
    Implements ICalculator(Of Double).Add
          
    Return n1 + n2
       
    End Function
       
    Public Function SubtractDouble(ByVal n1 As DoubleByVal n2 As DoubleAs Double _
             
    Implements ICalculator(Of Double).Subtract
          
    Return n1 - n2
       
    End Function
       
    Public Function MultiplyDouble(ByVal n1 As DoubleByVal n2 As DoubleAs Double _
             
    Implements ICalculator(Of Double).Multiply
          
    Return n1 * n2
       
    End Function
       
    Public Function DivideDouble(ByVal n1 As DoubleByVal n2 As DoubleAs Double _
             
    Implements ICalculator(Of Double).Divide
          
    Return n1 / n2
       
    End Function
       
    Public Function ConvertToDouble(ByVal n As ObjectAs Double _
             
    Implements ICalculator(Of Double).ConvertTo
          
    Return CDbl(n)
       
    End Function
    End Class
    Public Class StatsList(Of T, C As {New, ICalculator(Of T)})
       
    Inherits List(Of T)

       
    ' The object used as a calculator
       Dim calc As New C

       
    ' Return the sum of all elements.
       Public Function Sum() As T
          
    Dim result As T
          
    For Each elem As T In Me
             result 
    = calc.Add(result, elem)
          
    Next
          
    Return result
       
    End Function

       
    ' Return the average of all elements.
       Public Function Avg() As T
          
    Return calc.Divide(Me.Sum, calc.ConvertTo(Me.Count))
       
    End Function
    End Class


    Class Adder
       
    Implements IAdder(Of Integer), IAdder(Of Double)

       
    Public Function Add(ByVal n1 As IntegerByVal n2 As IntegerAs Integer _
             
    Implements IAdder(Of Integer).Add
          
    Return n1 + n2
       
    End Function

       
    Public Function Add(ByVal n1 As DoubleByVal n2 As DoubleAs Double _
             
    Implements IAdder(Of Double).Add
          
    Return n1 + n2
       
    End Function
    End Class
  • 相关阅读:
    rem适配布局---5. 方案1:苏宁首页制作1
    rem适配布局---4. rem适配方案
    rem适配布局---3. less
    rem适配布局---2. 媒体查询
    rem适配布局---1. 基础
    flex布局---9.携程网案例
    java基础---3. 数据类型转换、运算符
    flex布局---8.flex布局原理
    java基础---2. 常量&变量
    工会项目结题,游泳锻炼
  • 原文地址:https://www.cnblogs.com/callbin/p/1666941.html
Copyright © 2011-2022 走看看