2017-08-07 78 views
2

我正在VB.NET中構建一個應用程序,它將廣泛地使用類繼承,並將儘可能多的代碼放入基類中,包括數據庫操作。我遇到的問題是如何從父類中設置派生類的屬性。我可以檢索值,並設置非只讀屬性的值而不會出現問題,但是會出現幾種情況,我想通過代碼設置只讀屬性,但仍然不讓用戶更改這些屬性(例如修改日期/創建日期)。我已經組建了一個顯示問題的快速控制檯應用程序;從父類中更改子類只讀屬性

父類:

Public Class clsParent 
    Private _ParentInteger As Integer = 0 
    Public Property ParentInteger As Integer 
     Get 
      Return _ParentInteger 
     End Get 
     Set(value As Integer) 
      _ParentInteger = value 
     End Set 
    End Property 

    Public Sub PrintProperties() 
     Dim t As Type = Me.GetType 
     Console.WriteLine("Object type: '{0}'", t.ToString) 
     Dim propInfos As PropertyInfo() = t.GetProperties() 
     Console.WriteLine("The number of properties: {0}", propInfos.Length) 
     Console.WriteLine("------------------------------------------------------------") 
     For Each propInfo In propInfos 
      Dim readable As Boolean = propInfo.CanRead 
      Dim writable As Boolean = propInfo.CanWrite 

      Console.WriteLine(" Property name: {0}", propInfo.Name) 
      Console.WriteLine(" Property type: {0}", propInfo.PropertyType) 
      Console.WriteLine(" Read-Write: {0}", readable And writable) 
      Console.WriteLine(" Value:   {0}", propInfo.GetValue(Me)) 
      Console.WriteLine("------------------------------------------------------------") 
     Next 

    End Sub 

    Public Sub TryWriteProperties() 
     Dim t As Type = Me.GetType 
     Dim propInfos As PropertyInfo() = t.GetProperties() 

     For Each propInfo In propInfos 
      Console.WriteLine("------------------------------------------------------------") 
      Console.WriteLine(" {0}", propInfo.Name) 
      Try 
       Console.WriteLine("  Old Value:  {0}", propInfo.GetValue(Me)) 
       propInfo.SetValue(Me, CInt(propInfo.GetValue(Me)) + 100) 
       Console.WriteLine("  New Value:  {0}", propInfo.GetValue(Me)) 
      Catch ex As Exception 
       Console.WriteLine("  Failed to set new value: {0}", ex.Message) 
      End Try 
     Next 
    End Sub 
End Class 

而子類;

Public Class clsChild 
    Inherits clsParent 

    Dim _ChildWritableInteger As Integer = 5 
    Public Property ChildWritableInteger As Integer 
     Get 
      Return _ChildWritableInteger 
     End Get 
     Set(value As Integer) 
      _ChildWritableInteger = value 
     End Set 
    End Property 

    Dim _ChildReadOnlyInteger As Integer = 10 
    Public ReadOnly Property ChildReadOnlyInteger As Integer 
     Get 
      Return _ChildReadOnlyInteger 
     End Get 
    End Property 
End Class 

和一個簡單的子顯示輸出;

Sub Main() 
    Dim x As New clsChild 
    x.PrintProperties() 
    Console.WriteLine("Waiting...") 
    Console.ReadLine() 
    Do Until Console.ReadLine = "x" 
     x.TryWriteProperties() 
     Console.WriteLine("Waiting...") 
    Loop 
End Sub 

這個(非常正確)顯示錯誤「無法設置新值:未找到屬性集方法」。當它試圖設置「ChildReadOnlyInteger」屬性。

Object type: 'ConsoleApplication1.clsChild' 
The number of properties: 3 
------------------------------------------------------------ 
    Property name: ChildWritableInteger 
    Property type: System.Int32 
    Read-Write: True 
    Value:   5 
------------------------------------------------------------ 
    Property name: ChildReadOnlyInteger 
    Property type: System.Int32 
    Read-Write: False 
    Value:   10 
------------------------------------------------------------ 
    Property name: ParentInteger 
    Property type: System.Int32 
    Read-Write: True 
    Value:   0 
------------------------------------------------------------ 
Waiting... 


------------------------------------------------------------ 
    ChildWritableInteger 
     Old Value:  5 
     New Value:  105 
------------------------------------------------------------ 
    ChildReadOnlyInteger 
     Old Value:  10 
     Failed to set new value: Property set method not found. 
------------------------------------------------------------ 
    ParentInteger 
     Old Value:  0 
     New Value:  100 
Waiting... 

我的問題是,什麼是使用其中基礎類可以修改的財產,或子類的變量沒有公開暴露的屬性爲讀/寫,也不必連接到兩個性能的最佳方法相同的控股變量?

回答

2

我希望我能理解你的問題。您可以將「設置」設置爲受保護。

Class SomeName 

    Private _someVariable As Integer 

    Public Property SomeVariable As Integer 
     Get 
      Return _someVariable 
     End Get 
     Protected Set(value As Integer) 
      _someVariable = value 
     End Set 
    End Property 

End Class 
+0

感謝@the_lotus我沒有意識到我可以設置屬性的「部分」範圍 - 但我剛剛測試過,並且在初始測試中完美運行... – Bryant1003