因为vb.net中几乎相同的类具有约束

时间:2014-02-01 04:47:05

标签: .net vb.net oop

我有2个几乎相同的类,名为Key1和Key2。 这是Key1类的代码:

 Private Class Key1
    Implements IComparable(Of Key1)
    Private _Valor As String

    Public ReadOnly Property Valor() As String
        Get
            Return Me._Valor.Trim().ToUpper()
        End Get
    End Property

    Public Sub New(valor As String)
        Me._Valor = valor.Trim().ToUpper()
    End Sub

    Public Overrides Function GetHashCode() As Integer
        Dim ret As Integer = Me.Valor.GetHashCode()
        Return ret
    End Function

    Public Overrides Function Equals(obj As Object) As Boolean
        If IsNothing(obj) Then
            Return False
        ElseIf Not (TypeOf obj Is Key1) Then
            Return False
        ElseIf ReferenceEquals(obj, Me) Then
            Return True
        ElseIf obj.GetHashCode() <> Me.GetHashCode() Then
            Return False
        End If

        Dim tmp As Key1 = DirectCast(obj, Key1)

        If String.IsNullOrEmpty(tmp.Valor) Then
            Return False
        ElseIf String.IsNullOrEmpty(Me.Valor) Then
            Return False
        Else
            Return Me.Valor = tmp.Valor
        End If

    End Function

    Public Function CompareTo(ByVal other As Key1) As Integer Implements System.IComparable(Of Key1).CompareTo
        Return Me.Valor.CompareTo(other.Valor)
    End Function

    Public Overrides Function ToString() As String
        Return Me.Valor.ToString()
    End Function

    Public Function Clonar() As Key1
        Return New Key1(Me.Valor)
    End Function
End Class

Key2代码几乎与Key2的替代Key1相同:

  1. 将Key1代码复制到notepad.exe
  2. 编辑 - &GT;替换
  3. 查找内容:Key1
  4. 替换为:Key2
  5. 如果不按以下约束重复这些代码,我该怎么做:

    • Key1不能用于代替Key2
    • Key2不能用于代替Key1

2 个答案:

答案 0 :(得分:0)

你的意思是这样的:

Class KeyBase(Of T)
    Implements IComparable(Of KeyBase(Of T))

    Private _Valor As String

    Public ReadOnly Property Valor() As String
        Get
            Return Me._Valor.Trim().ToUpper()
        End Get
    End Property

    Public Sub New(valor As String)
        Me._Valor = valor.Trim().ToUpper()
    End Sub

    Public Overrides Function GetHashCode() As Integer
        Dim ret As Integer = Me.Valor.GetHashCode()
        Return ret
    End Function

    Public Overrides Function Equals(obj As Object) As Boolean
        If IsNothing(obj) Then
            Return False
        ElseIf Not (TypeOf obj Is KeyBase(Of T)) Then
            Return False
        ElseIf ReferenceEquals(obj, Me) Then
            Return True
        ElseIf obj.GetHashCode() <> Me.GetHashCode() Then
            Return False
        End If

        Dim tmp As KeyBase(Of T) = DirectCast(obj, KeyBase(Of T))

        If String.IsNullOrEmpty(tmp.Valor) Then
            Return False
        ElseIf String.IsNullOrEmpty(Me.Valor) Then
            Return False
        Else
            Return Me.Valor = tmp.Valor
        End If

    End Function

    Public Function CompareTo(ByVal other As KeyBase(Of T)) As Integer Implements System.IComparable(Of KeyBase(Of T)).CompareTo
        Return Me.Valor.CompareTo(other.Valor)
    End Function

    Public Overrides Function ToString() As String
        Return Me.Valor.ToString()
    End Function

    Public Function Clonar() As KeyBase(Of T)
        Return New KeyBase(Of T)(Me.Valor)
    End Function
End Class

Class Key1
    Inherits KeyBase(Of Key1)

    Public Sub New(valor As String)
        MyBase.New(valor)
    End Sub
End Class

Class Key2
    Inherits KeyBase(Of Key2)

    Public Sub New(valor As String)
        MyBase.New(valor)
    End Sub
End Class

答案 1 :(得分:0)

我可以将代码改为:

Public Class Key1
    Inherits Keys(Of Key1)
    Public Sub New(valor As String)
        MyBase.New(valor)
    End Sub
    Public Overrides Function Clonar() As Key1
        Return New Key1(Me.Valor)
    End Function
End Class

Public Class Key2
    Inherits Keys(Of Key2)
    Public Sub New(valor As String)
        MyBase.New(valor)
    End Sub
    Public Overrides Function Clonar() As Key2
        Return New Key2(Me.Valor)
    End Function
End Class

您需要创建以下界面&amp;使这项工作的基类:

Public Interface IKey
    ReadOnly Property Valor() As String
End Interface

Public MustInherit Class Keys(Of K As IKey)
    Implements IComparable(Of K)
    Implements IKey
    Private _Valor As String

    Public ReadOnly Property Valor() As String Implements IKey.Valor
        Get
            Return Me._Valor.Trim().ToUpper()
        End Get
    End Property

    Public Sub New(valor As String)
        Me._Valor = valor.Trim().ToUpper()
    End Sub

    Public Overrides Function GetHashCode() As Integer
        Dim ret As Integer = Me.Valor.GetHashCode()
        Return ret
    End Function

    Public Overrides Function Equals(obj As Object) As Boolean
        If obj Is Nothing Then
            Return False
        ElseIf Not (TypeOf obj Is K) Then
            Return False
        ElseIf ReferenceEquals(obj, Me) Then
            Return True
        ElseIf obj.GetHashCode() <> Me.GetHashCode() Then
            Return False
        End If

        Dim tmp As K = DirectCast(obj, K)

        If String.IsNullOrEmpty(tmp.Valor) Then
            Return False
        ElseIf String.IsNullOrEmpty(Me.Valor) Then
            Return False
        Else
            Return Me.Valor = tmp.Valor
        End If

    End Function

    Public Function CompareTo(ByVal other As K) As Integer Implements System.IComparable(Of K).CompareTo
        Return Me.Valor.CompareTo(other.Valor)
    End Function

    Public Overrides Function ToString() As String
        Return Me.Valor.ToString()
    End Function

    Public MustOverride Function Clonar() As K

End Class