2013-02-21 52 views
3

以下类用于ASP.NET应用程序从数据库结果集中读取货币并将其添加(例如,以美元显示总计加显示总计以GB磅为单位)。它的工作原理以下方式:继承一个类以删除一个属性并更改方法逻辑

  1. 读货币ID值
  2. 如果货币ID已经存在,增加了总该货币
  3. 如果货币ID不存在,将其添加到它的值列表
  4. 下一页

它的工作原理以及使用CurrencyID财产为每一个独特的货币之间的区别。但是,现在已经很明显,默认情况下,IsoCurrencySymbol对于每种货币也是唯一的,所以CurrencyID实际上并不需要。

所以...我想知道是否可以从这个类继承并删除任何对CurrencyID的引用,因此使CompareTo方法使用IsoCurrencySymbol来代替。

诀窍是将现有的类广泛使用,但引入一个不需要CurrencyID的修改版本。这可能吗?

<Serializable()> _ 
Public Class CurrencyCounter 

    <Serializable()> _ 
    Private Class CurrencyType 
     Implements IComparable 

     Public IsoCurrencySymbol As String 
     Public CurrencySymbol As String 
     Public CurrencyID As Int16 
     Public Amount As Decimal 

     Public Function CompareTo(obj As Object) As Integer Implements System.IComparable.CompareTo 
      If Not TypeOf (obj) Is CurrencyType Then 
       Throw New ArgumentException("Object is not a currency type") 
      Else 
       Dim c2 As CurrencyType = CType(obj, CurrencyType) 
       Return Me.CurrencyID.CompareTo(c2.CurrencyID) 
      End If 
     End Function 

    End Class 

    Private _Currencies As List(Of CurrencyType) 

    Public Sub New() 
     _Currencies = New List(Of CurrencyType) 
    End Sub 

    Private Sub AddStructToList(CurrencyID As Integer, IsoCurrencySymbol As String, CurrencySymbol As String, Amount As Decimal) 
     If IsoCurrencySymbol <> String.Empty AndAlso Amount > 0 Then 
      Dim s As New CurrencyType 
      s.CurrencyID = CurrencyID 
      s.IsoCurrencySymbol = IsoCurrencySymbol 
      s.CurrencySymbol = CurrencySymbol 
      s.Amount = Amount 
      _Currencies.Add(s) 
     End If 
    End Sub 

    Public Sub Add(CurrencyID As Integer, IsoCurrencySymbol As String, CurrencySymbol As String, Amount As Decimal) 
     Dim ct As CurrencyType = _Currencies.Find(Function(obj) obj.CurrencyID = CurrencyID) 
     If ct IsNot Nothing Then 
      ct.Amount += Amount 
     Else 
      AddStructToList(CurrencyID, IsoCurrencySymbol, CurrencySymbol, Amount) 
     End If 
    End Sub 

    Public Sub Clear() 
     _Currencies.Clear() 
    End Sub 

    Public Function Count() As Integer 
     Return _Currencies.Count 
    End Function 

    Public Function RenderTotals() As String 
     ' ... 
    End Function 

End Class 

回答

4

不,你不能这样做。为了确保所有派生类(如果没有其他的)至少与它们的基类共享相同的公共接口,继承它的全部要点。如果你正在移除一个属性,那么它不会共享相同的接口,因此它是不兼容的,并且不是继承的候选对象。

如果您不能说派生类是基类的类型,那么您不能使用继承。例如,我可以说汽车是一种类型的汽车,因此,如果我有汽车类,我可以从汽车类继承。但我不能说昆虫是一种车辆。因此,即使它们共享大多数事物,我也不能从车辆类继承昆虫类。

此限制的原因是因为继承允许您将对象视为基本类型(通过类型铸造)。例如:

Public Sub AddPassengerToVehicle(v As Vehicle) 
    v.Passengers.Add(New Passenger()) 
End Sub 

' ... 

Dim auto As New Automobile() 
Dim bug As New Insect() 
AddPassengerToVehicle(auto) ' Works because an automobile is a type vehicle (inherits from vehicle) 
AddPassengerToVehicle(bug) ' Can't possibly work (nor should it) 

所以,如果你是在你需要有一个派生类中删除的情况/隐藏其基类的成员之一,你在错误的方向走去。在这样的情况下,你需要创建一个全新的类,它恰好有一个非常类似的界面,但与第一类没有直接的关系,例如:

Public Class Vehicle 
    Public Property Passengers As List(Of Passenger) 
    Public Property MaxSpeed As Integer 

    Public Function SpeedIsTooFast(speed) As Boolean 
     Return (speed > MaxSpeed) 
    End Function 
End Class 

Public Class Insect 
    Public Property MaxSpeed As Integer 

    Public Function SpeedIsTooFast(speed) As Boolean 
     Return (speed > MaxSpeed) 
    End Function 
End Class 

如果你想分享功能,比如上面例子中的SpeedIsTooFast方法中的逻辑,那么有几种不同的方法可以做到这一点。这首先是使包装方法,它只是使其他类调用,例如:

Public Class Insect 
    Private _vehicle As New Vehicle() 

    Public Property MaxSpeed() As Integer 
     Get 
      Return _vehicle.MaxSpeed 
     End Get 
     Set(value As Integer) 
      _vehicle.MaxSpeed = value 
     End Set 
    End Property 

    Public Function SpeedIsTooFast(speed) As Boolean 
     Return _vehicle.SpeedIsTooFast(speed) 
    End Function 
End Class 

如果你做这种方式,这将是最好有两个类实现相同的通用接口,这样你可以互换在必要时使用它们,例如:

Public Interface ISelfPoweredMovingThing 
    Property MaxSpeed As Integer 
    Function SpeedIsTooFast(speed As Integer) As Boolean 
End Interface 

另一种选择是打出来的公共功能到第三类,然后使用这个类为基准,为其他两个,比如:

Public Class SelfPoweredMovingThing 
    Public Property MaxSpeed As Integer 

    Public Function SpeedIsTooFast(speed) As Boolean 
     Return (speed > MaxSpeed) 
    End Function 
End Class 

Public Class Vehicle 
    Inherits SelfPoweredMovingThing 

    Public Property Passengers As List(Of Passenger) 
End Class 

Public Class Insect 
    Inherits SelfPoweredMovingThing 

    ' Anything else specific only to insects... 
End Class 
+2

嗨史蒂文。这是一个非常好的答案。非常感谢您抽出时间将其分解到我的外行人员身上:-) – EvilDr 2013-02-21 16:30:30

相关问题