看起来另一个问题的答案会起作用:这是一个示例:
Public Interface IReadOnly
ReadOnly Property Name() As String
End Interface
Public Interface IReadWrite
Inherits IReadOnly
Overloads Property Name() As String
End Interface
Public Class ReadOnlyClass
Implements IReadOnly
Private _Name
Public ReadOnly Property Name() As String Implements IReadOnly.Name
Get
Return _Name
End Get
End Property
End Class
Public Class ReadWriteClass
Implements IReadWrite
Private ReadOnly Property ReadOnly_Name() As String Implements IReadOnly.Name
Get
Return Name
End Get
End Property
Private _Name As String
Public Overloads Property Name() As String Implements IReadWrite.Name
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
End Class
上述方法实际上会导致实现 IReadWrite 的类也实现 IReadOnly——因此您实际上需要向下转换为 IReadWrite 才能设置属性。
另一种方法可以避免该问题,但在实现类及其调用者中需要更多逻辑,例如:
Public Interface ISometimesWritable
Property Name() As String
ReadOnly Property AllowNameEdit() As Boolean
End Interface
Public Class ReadOnlyClass
Implements ISometimesWritable
Public ReadOnly Property AllowNameEdit() As Boolean Implements ISometimesWritable.AllowNameEdit
Get
Return False
End Get
End Property
Private _Name As String
Public Property Name() As String Implements ISometimesWritable.Name
Get
Return _Name
End Get
Set(ByVal value As String)
Throw New NotSupportedException("Name cannot be set when AllowNameEdit is False")
End Set
End Property
End Class
Public Class ReadWriteClass
Implements ISometimesWritable
Public ReadOnly Property AllowNameEdit() As Boolean Implements ISometimesWritable.AllowNameEdit
Get
Return True
End Get
End Property
Private _Name As String
Public Property Name() As String Implements ISometimesWritable.Name
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
End Class
更新:回答关于向下转换的问题; “向下转换”是一个术语,用于描述将对象从超类、接口或抽象基类Type 转换为更具体的Type。
例如,上面的第一个示例定义了两个接口:IReadOnly 和IReadWrite。您会注意到IReadWrite 实现了IReadOnly,这意味着您可以同时对实现IReadWrite 的对象进行IReadWrite 和 IReadOnly 调用。
由于IReadWrite 实现了IReadOnly,因此IReadWrite 被称为IReadOnly 的“子类”(尽管“子类”更准确地用于描述继承 一个基类,而不是 实现 一个接口——为了简单起见,它们几乎是相同的概念)。如果IReadWrite 是IReadOnly 的子类,则反之亦然——IReadOnly 是IReadWrite 的超类。
例如,我可以将ReadWriteClass 的实例描述为任一接口的实现:
Public Sub SomeMethod()
dim readOnlyInstance as IReadOnly = new ReadWriteClass()
Console.WriteLine(readOnlyInstance.Name)
' The following line won't compile, since we're communicating with ReadWriteClass as an instance of IReadOnly
'readOnlyInstance.Name = "Santa Clause"
' Here we downcast the variable to reference it by it's other interface, IReadWrite
dim readWriteInstance = DirectCast(readOnlyInstance, IReadWrite)
' Now we can both Get and Set the value of Name
readWriteInstance.Name = "John Doe"
Console.WriteLine(readWriteInstance.Name)
' Note that in the above example we created *one* instance of ReadWriteClass
' and have provided two variables / references to the same underlying object.
Console.WriteLine(readOnlyInstance.Name) ' <-- note that this should return "John Doe"
End Sub