我在VB.NET中构建一个将广泛使用类继承的应用程序,并将尽可能多的代码放入基类,包括数据库操作。我遇到的问题是如何从父类设置派生类的属性。我可以检索这些值,并设置非readonly属性的值而不会出现问题,但是有几种情况我想通过代码设置只读属性,但仍然不允许用户更改这些(修改日期/创建日期,例如)。我已经整理了一个显示问题的快速控制台应用程序;
父类:
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...
我的问题是,什么是最好的方法,基类可以修改子类的属性或变量,而不会将属性公开显示为读/写,并且没有两个属性链接到同一个保持变量?
答案 0 :(得分:2)
我希望我理解你的问题。您可以设置" Set"作为受保护的。
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