2013-02-21 36 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

相關問題